这似乎很容易做到,但我在挣扎。编程不是我的主要背景,所以我缺少很多基础知识,但我正在努力学习。在
我要处理的问题是我想使用一个Tkinter按钮来显示一个按钮列表(目前只有一个),当其中一个按钮被按下时,它将所述按钮的文本输入到一个字符串变量中,关闭按钮窗口,然后继续代码。在
以下是我为本节准备的:
root = tk.Tk()
def data(name):
global query
query = name
B = tk.Button(root, text ='LogID', command = data('LogID'))
B.pack()
root.mainloop()
print query
如果这看起来有点杂乱无章,那是因为它确实如此。在
这部分前面有代码,后面有代码。我想把窗户关上(根目录。销毁())当按下按钮时,代码将从“query”打印文本,因此我知道它已将值传递给它。在
当我运行它时,它挂在根.mainloop()节,或似乎是。老实说,我不完全理解代码中的功能,我只知道它需要它。在
因为我看到您已经在使用全局变量,
我将用一个
如何使应用程序成为Tkinter.Tk.塔卡(tkinter.Tk.塔卡在python 3中)。在
变量自我数据可以由您的类的任何方法使用;
因此,您不必使用global关键字。
mainloop
使tkinter应用程序“运行”并处理事件。请注意,我没有销毁第一个窗口,而是使用
withdraw
方法将其隐藏起来。这只是你可能感兴趣的另一个选择。您可以使用
deiconify
使其可见(更多信息here)。第二个窗口是一个顶层小部件,您可以使用它来创建辅助窗口。在我写了一个更有教育意义的例子here。在
相关问题 更多 >
编程相关推荐