您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-为什么Tkinter Entry的get函数什么都不返回?

Python-为什么Tkinter Entry的get函数什么都不返回?

看起来你可能对何时运行命令感到困惑。在你的示例中,你是get在GUI有机会在屏幕上显示之前调用方法的(在调用之后发生)mainloop

尝试添加一个调用该get方法的按钮。如果你将应用程序编写为类,这会容易得多。例如:

import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()

    def on_button(self):
        print(self.entry.get())

app = SampleApp()
app.mainloop()

运行程序,输入条目小部件,然后单击按钮。

python 2022/1/1 18:28:53 有215人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶