为什么当我使用 tkinter 配置选项时这个标签没有改变 [英] Why isn't this label changing when I use the tkinter config option

查看:39
本文介绍了为什么当我使用 tkinter 配置选项时这个标签没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改此标签,因此我使用了 widget.config 选项,但由于某种原因它不起作用.这是我的代码:

I want to change this label so I used the widget.config option but it's not working for some reason. Here's my code:

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()

推荐答案

在使用 config 功能时,您必须提及要更改的内容.尝试:-answer_label.config(text=您要显示的文本")

While using the config function, you have to mention what is it that you want to change. try:- answer_label.config(text="Text that you want to be displayed")

此外,您还没有从 Entry 小部件中获取值:为此,您可以使用:answer_label.config(text=main_entry.get())

Also you have not fetched the value from the Entry widget: For that, you can use: 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()

这篇关于为什么当我使用 tkinter 配置选项时这个标签没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆