【发布时间】:2019-09-26 13:05:13
【问题描述】:
我想使用最有效的方法来限制用户只能输入数字。例如:当他们在条目中输入字母时,该确切条目将被清除。有没有什么方法可以做到结构上的变化最少?
这是我的代码:[the ???????标志是我卡住的地方]
import tkinter as tk
from tkinter import ttk
class App:
"""Main class for a simple application"""
def __init__(self, master):
"""Main initialisation for the main app"""
# create a top frame or window which we can add things to it
self.topFrame = tk.Frame(master)
self.topFrame.pack()
# create a holder (notebook) for tabs
self.note = ttk.Notebook(self.topFrame)
# create four different tabs for the main screen
self.tab1 = ttk.Frame(self.note)
self.tab2 = ttk.Frame(self.note)
self.tab3 = ttk.Frame(self.note)
self.tab4 = ttk.Frame(self.note)
# add four different tabs for the main screen with various names
self.note.add(self.tab1, text=' Demography ')
self.note.add(self.tab2, text=' Urban Economy ')
self.note.add(self.tab3, text=' Land Use ')
self.note.add(self.tab4, text=' Urban Transportation ')
self.note.pack()
# add a frame to each tab
self.demog_frame = tk.Frame(self.tab1)
self.demog_frame.pack()
self.eco_frame = tk.Frame(self.tab2)
self.eco_frame.pack()
self.land_frame = tk.Frame(self.tab3)
self.land_frame.pack()
self.trans_frame = tk.Frame(self.tab4)
self.trans_frame.pack()
# add label frames to {DEMOGRAPHY TAB}
self.demog_flable_1 = tk.LabelFrame(self.demog_frame, text='Population Growth:')
self.demog_flable_1.grid(row=0, column=0, ipadx=0, ipady=10, padx=0)
# add info to the first LabelFrame
self.d_p_grwt_label_fy = tk.Label(self.demog_flable_1, text='population of year n |', pady=0, font='Arial 8')
self.d_p_grwt_label_fy.grid(row=0, column=0)
self.d_p_grwt_label_sy = tk.Label(self.demog_flable_1, text='population of year n+1 |', font='Arial 8')
self.d_p_grwt_label_sy.grid(row=0, column=1)
self.d_p_grwt_label_fyy = tk.Label(self.demog_flable_1, text='year n eg:1999 |', font='Arial 8')
self.d_p_grwt_label_fyy.grid(row=2, column=0)
self.d_p_grwt_label_syy = tk.Label(self.demog_flable_1, text='year n+1 eg:2006 |', font='Arial 8')
self.d_p_grwt_label_syy.grid(row=2, column=1)
self.d_p_grwt_entry_fy = tk.Entry(self.demog_flable_1, width=12)
self.d_p_grwt_entry_fy.bind("<KeyRelease>", func=self.popfunc_1)
self.d_p_grwt_entry_fy.grid(row=1, column=0)
self.d_p_grwt_entry_sy = tk.Entry(self.demog_flable_1, width=12)
self.d_p_grwt_entry_sy.grid(row=1, column=1)
self.d_p_grwt_entry_fyy = tk.Entry(self.demog_flable_1, width=12)
self.d_p_grwt_entry_fyy.grid(row=3, column=0)
self.d_p_grwt_entry_syy = tk.Entry(self.demog_flable_1, width=12)
self.d_p_grwt_entry_syy.grid(row=3, column=1)
#----------------------------------------------
self.d_p_grwt_dscrpt_label = tk.Label(self.demog_flable_1, text='population absolute change \u2193', font='Arial 8')
self.d_p_grwt_dscrpt_label.grid(row =0, column=2)
self.d_p_grwt_dscrpt_label1 = tk.Label(self.demog_flable_1, text='population annual absolute change \u2193', font='Arial 8')
self.d_p_grwt_dscrpt_label1.grid(row =2, column=2)
self.d_p_grwt_dscrpt_label2 = tk.Label(self.demog_flable_1, text='population annual percent change \u2193', font='Arial 8')
self.d_p_grwt_dscrpt_label2.grid(row =4, column=2)
def popfunc_1(self, *args):
try:
p1 = int(self.d_p_grwt_entry_fy.get())
p2 = int(self.d_p_grwt_entry_sy.get())
y1 = int(self.d_p_grwt_entry_fyy.get())
y2 = int(elf.d_p_grwt_entry_syy.get())
print(x1)
except ValueError:
????
root = tk.Tk()
root.title('Urban Calculator')
root.geometry('420x200')
app = App(root)
root.mainloop()
在课程的底部,我尝试通过ValueError 排除字符串。如果我写一个pass 很好,但我也希望清除该条目。有没有办法指向p1 或p2 并说出导致错误被清除的任何一个?
【问题讨论】:
-
单个
try-except不会为您提供有关哪个条目导致问题的信息。每个条目都需要一个单独的条目。 -
谢谢。有没有更懒惰的方式来做到这一点?
-
您是想阻止输入字符,还是让用户输入他们想要的任何内容,然后您稍后验证输入? Tkinter 有一种方法可以防止用户输入数字以外的任何内容。见stackoverflow.com/a/4140988/7432
-
谢谢,但我知道如何限制它。我希望在用户输入 alpha 后稍稍延迟清除它。
-
为什么要延迟?
标签: python python-3.x tkinter tkinter-entry