我想用最有效的方法来限制用户只输入数字。例如:当他们在一个条目中输入一个字母时,该条目将被清除。有没有什么方法可以使结构的变化最小
这是我的密码?????我被困的地方]
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
并说是哪个导致错误被清除的
是的,但不是一下子。您必须分别对每个条目运行You try/except
例如,可以将条目字段加载到列表中,然后使用循环将每个条目绑定到方法,然后使用传递的事件与小部件进行交互
看下面的例子,让我知道如果你有任何问题
相关问题 更多 >
编程相关推荐