基于用户偏好的两个按钮调用函数的应用程序
您好,谢谢您的帮助!我是一个使用tkinter的相对初学者,并且在堆栈溢出方面完成了一些教程和一些问题。但是,在用户按下按钮后,我无法在GUI窗口中生成回调函数的输出。守则概述如下:
root = tk.Tk()
root.title('Crowdsourcing creativity')
class Application(tk.Frame):下面我尝试创建一个全屏应用程序,它可以通过转义键退出。
def __init__(self, master=None):
super().__init__(master)
self.master = master
pad=3
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.close_window)
self.pack()
self.create_widgets()
def close_window(self,event):
root.destroy()接下来,我创建了两个按钮
def create_widgets(self):
prompt_button = tk.Button(self,text = 'Do something', command=self.give_prompt).pack(side=tk.LEFT)
location_button = tk.Button(self, text = 'Go somewhere', command=self.give_location).pack(side=tk.RIGHT)最后,我尝试为按钮定义回调函数。注意,提示符()和location()引用了代码中前面定义的函数(本质上是从它们各自的列表中返回一个随机项)。
def give_prompt(self):
label1 = Label(self, prompt())
label1.pack()
def give_location(self):
label2 = Label(self, location())
label2.pack()
app = Application(master=root)
app.mainloop()输出当前出现在我运行代码的终端中,而不是GUI。我还希望原始按钮在单击时消失,因此输出是GUI中唯一保留的东西。任何关于如何解决这个问题的建议都将不胜感激!谢谢
发布于 2021-04-05 20:35:31
使用tk.Label。
或者不是进口tkinter,而是这样做:
from tkinter import *
https://stackoverflow.com/questions/61480478
复制相似问题