[英]Tkinter widget where User chooses parameter values, but how do I store these values?
My objective is to create three variables called 'Number of pipes','Anisotropy ratio', 'Filter depth (in cm)' which values are chosen by the user (through a User interface).我的目标是创建三个变量,称为“管道数”、“各向异性比”、“过滤器深度(以厘米为单位)”,这些值由用户(通过用户界面)选择。 I achieved to create the scrollbar of Tkinter and to enter values but these are not stored as variables.
我实现了创建 Tkinter 的滚动条并输入值,但这些值不存储为变量。 Could someone give me a hand solving that?
有人可以帮我解决这个问题吗? I am new in Python and specially I am struggling with Tkinter.
我是 Python 新手,特别是我在用 Tkinter 苦苦挣扎。 The code is developped in Python 3.7:
代码是用 Python 3.7 开发的:
root = tk.Tk()
root.geometry('1000x1000')
#Description show in window
info=tk.Label(root,anchor='e')
info.pack()
#Parameters
parameters = ('Number of pipes','Anisotropy ratio', 'Filter depth (in cm)')
def ask_parameter(entry):
user_pipes = str (entry['Number of pipes'].get())
user_aniso = str (entry['Anisotropy ratio'].get()) #effective screen length = b
user_depth = str (entry['Filter depth (in cm)'].get())
print(user_pipes,user_aniso,user_depth)
#if parameters
# return True
# else:
# tkinter.messagebox.showwarning ('Only numbers', 'Try again')
# return True
#
def form(parameters):
entry = {}
for parameter in parameters:
print(parameter)
row = tk.Frame(root)
lab = tk.Label(row, width=15, text=parameter+": ", anchor='w')
ent = tk.Entry(row)
row.pack(side=tk.TOP,
fill=tk.X,
padx=2,
pady=2)
lab.pack(side=tk.LEFT)
ent.pack(side=tk.RIGHT, expand=tk.YES,fill=tk.X)
entry[parameter] = ent
return entry
if __name__ == '__main__':
ents = form(parameters)
save_button = tk.Button(root)
save_button.configure(text='Save', command=lambda: ask_parameter(ents))
save_button.pack()
root.mainloop()
Any problem is shown with the code but the parameters are not stored as variables with the entered values.代码会显示任何问题,但参数不会存储为具有输入值的变量。 Thank you very much for your time.
非常感谢您的宝贵时间。
您的代码将通过 GUI 输入的值存储到变量中,您的问题可能是您的变量不是全局变量,因此您将无法在创建它们的函数之外使用它们。您可以解决这个问题带有return
,或者通过将global varname
放在每个变量的定义之上以及每个将使用它的函数的开头来使它们成为全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.