首页 > 解决方案 > 为什么当我使用配置选项时这个标签没有改变?

问题描述

因此,当您使用 config 和 if 语句将内容放入此条目时,我有点希望这个小型机器人能够与您交谈并做出响应,但它似乎并没有真正起作用。我现在的问题是,当我进行配置时,这里的标签正在改变。这是我的代码:

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
    if main_entry == "Hey":
        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 = "Hello!").pack()
frame.place(relx=.5, rely=.5, anchor='center')

root.mainloop()

问题是 if 语句不起作用,它没有改变 ext,但我也没有收到任何错误消息,所以我有点困惑。

标签: pythonpython-3.xif-statementtkinterpycharm

解决方案


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():
    getanswer=main_entry.get()
    if getanswer == "Hey":
        answer_label.configure(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 = "Hello!")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')

root.mainloop()

推荐阅读