【问题标题】:Why is this label not changing when I use the config option? [duplicate]为什么当我使用配置选项时这个标签没有改变? [复制]
【发布时间】:2021-06-29 11:43:31
【问题描述】:

所以,当您使用 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,但我也没有收到任何错误消息,所以我有点困惑。

【问题讨论】:

    标签: python python-3.x if-statement tkinter pycharm


    【解决方案1】:
    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()
    

    【讨论】:

    • 它显示一条错误消息:第 13 行,在 answer_label.configure(text='Hi') AttributeError: 'NoneType' object has no attribute 'configure'
    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2021-06-30
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多