pycharm 为什么在使用tkinter配置选项时此标签没有更新?

kcugc4gi  于 2023-05-29  发布在  PyCharm
关注(0)|答案(2)|浏览(135)

我想更新这个标签,所以我使用了answer_label.config选项,但它不适用于某些情况。
下面是我的代码:

import tkinter as tk

root = tk.Tk()
root.attributes('-fullscreen', True)

exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)

def answer():
    global main_entry
    answer_label.config(main_entry)

frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey").pack()
frame.place(relx=.5, rely=.5, anchor='center')

root.mainloop()
2uluyalo

2uluyalo1#

在使用config函数时,你必须提到你想改变什么。try:- answer_label.config(text="Text that you want to be displayed")
此外,您还没有从Entry小部件中获取值:为此,您可以用途:answer_label.config(text=main_entry.get())
完整的代码看起来像这样:

import tkinter as tk

root = tk.Tk()
root.attributes('-fullscreen', True)

exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)

def answer():
    answer_label.config(text=main_entry.get())

frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')

root.mainloop()
nlejzf6q

nlejzf6q2#

这个代码可以工作。它配置标签以更改文本。我不知道你想用config(main_entry)实现什么。

import tkinter as tk

root = tk.Tk()
root.attributes('-fullscreen', True)

exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)

def answer():
    global main_entry, answer_label
    answer_label.config(text="hi")

frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')

root.mainloop()

相关问题