我想创建一个tkinter小部件,在其中为不同的参数手动添加值。
import tkinter as tk
import tkinter.messagebox
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
#Size of the window
root = tk.Tk()
root.geometry('1000x1000')
#Scrollbar
scrollbar = tk.Scrollbar(root)
scrollbar.pack( side = 'right', fill = 'y' )
#Description show in window
info=tk.Label(root)
info.pack()
#Parameters
parameters = ('Number of pipes', 'Anisotropy ratio', 'Filter depth (in cm)')
def ask_parameter():
user_pipes = float (entry['num_pipes (default = 2)'].get()) #effective radius well casing (1.685), corrected for radius transponder (0.3)
if user_pipes == entry['num_pipes (default = 2)'].get():
num_pipes = user_pipes
else:
num_pipes = 2
user_aniso = float (entry['aniso (default = 1)'].get()) #effective screen length = b
if user_aniso == entry['aniso (default = 1)'].get():
aniso = user_aniso
else:
aniso = 1
user_depth = float (entry['filter_depth (default = 20)'].get()) #effective radius slug test = effective radius well screen (see fetter p. 194)
if user_depth == entry['filter_depth (default = 20)'].get():
filter_depth = user_depth
else:
filter_depth = 20
def form():
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__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
save_button = tk.Button(root)
save_button.configure(text='Save', command=ask_parameter)
save_button.pack()
root.mainloop()
我没有实现用user_pipes,user_aniso和user_depth的值创建三个变量。 我遇到下一个错误:
return self.func(*args), TypeError: ask_parameter() missing 1 required positional argument: 'entry'
我想用用户选择的值创建3个变量。
现在,我添加了您的修改并给出了下一个错误: TclError:无法调用“按钮”命令:应用程序已被破坏