Python/Tkinter:从子窗口读取输入
我正在准备一个使用Python和Tkinter的GUI应用程序(我是这门语言的新手) 我有一个主窗口和一个配置子窗口,其中包含我打开时使用的一些文本参数:Python/Tkinter:从子窗口读取输入,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在准备一个使用Python和Tkinter的GUI应用程序(我是这门语言的新手) 我有一个主窗口和一个配置子窗口,其中包含我打开时使用的一些文本参数: def config_open(): global wdw, e wdw = Toplevel() wdw.geometry('+400+400') w = Label(wdw, text="Parameter 1", justify=RIGHT) w.grid(row=1, column=0) e = Entry(
def config_open():
global wdw, e
wdw = Toplevel()
wdw.geometry('+400+400')
w = Label(wdw, text="Parameter 1", justify=RIGHT)
w.grid(row=1, column=0)
e = Entry(wdw)
e.grid(row=1, column=1)
e.focus_set()
然后我添加了一个“确定”按钮,该按钮调用:
def config_save():
global wdw, e
user_input = e.get().strip()
print user_input
这是可行的,但我宣布一切都是全球性的。有没有更好的方法来引用子窗口中的元素?哪里是
ok
按钮?在主窗口或配置窗口中?这可能对您有用:确定按钮位于配置窗口内。
from Tkinter import *
def config_open():
wdw = Toplevel()
wdw.geometry('+400+400')
# Makes window modal
wdw.grab_set()
# Variable to store entry value
user_input = StringVar()
Label(wdw, text="Parameter 1", justify=RIGHT).grid(row=1, column=0)
e = Entry(wdw, textvariable=user_input)
e.grid(row=1, column=1)
e.focus_set()
Button(wdw, text='Ok', command=wdw.destroy).grid(row=2, column=1)
# Show the window and wait for it to close
wdw.wait_window(wdw)
# Window has been closed
data = {'user_input': user_input.get().strip(),
'another-option': 'value of another-option'}
return data
class App:
def __init__(self):
self.root = Tk()
self.root.geometry('+200+200')
self.label_var = StringVar()
self.user_input = None
Button(self.root, text='Configure', command=self.get_options).place(relx=0.5, rely=0.5, anchor=CENTER)
Label(self.root, textvariable=self.label_var).place(relx=0.5, rely=0.3, anchor=CENTER)
self.root.mainloop()
def get_options(self):
options = config_open()
self.user_input = options['user_input']
self.label_var.set(self.user_input)
App()