from tkinter import *
root = Tk()
root.title('printer')
label = Label(root, text = "WELCOME", fg = "grey")
label.pack()
textbox = Entry(root)
textbox.pack()
def whenclicked():
global hello
hello = "textbox.get()"
label1 = Label(root, text = hello)
label1.pack()
button = Button(root, text = "print it", command = whenclicked())
button.pack()
root.mainloop()
当我运行此代码时,在单击按钮之前,输出已经存在。这有什么问题?
看来标签可能是mainloop的一部分,这可能导致它与其余代码分开工作,而没有交互,将其从mainloop中移出。