gpt4 book ai didi

python - 条目小部件内容不随函数调用而改变

转载 作者:行者123 更新时间:2023-12-01 04:31:44 25 4
gpt4 key购买 nike

每当显示 tkinter 框架时,我都需要更改条目的内容。以下是我到目前为止所拥有的,但似乎不起作用。我尝试使用 data = self.read() 然后使用 now.insert(0, data) 但这也不起作用。如果显示该值,则每次调用类 ReadLabel1 时该值都不会更改。

class ReadLabel1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="blue")

label = tk.Label(self, text="SomeData:", font = "Times 12", bg="blue")
label.place(x=10, y=100) #ack(pady=5,padx=30)

self.smStr = tk.StringVar()
now=tk.Entry(self, width=22, textvariable=self.read())
now.place(x=120, y=103)

def read(self):
# code to get data
return data

最佳答案

您需要将“更改条目的内容”转换为单参数回调,将“每当显示 tkinter 框架”转换为事件,然后将应用程序、事件和回调绑定(bind)在一起。这是一个最小的例子。

import time
import tkinter as tk

root = tk.Tk()
now = tk.StringVar()
lab = tk.Label(root, textvariable=now)
lab.pack()
def display_now(event):
now.set(time.ctime())
root.bind('<Visibility>', display_now)
root.bind('<FocusIn>', display_now)

将窗口最小化为图标并将其恢复会触发 Visibility 事件。用不同的窗口覆盖和仅仅揭开则不然,至少在 Windows 中不行。单击未覆盖或仅未激活的窗口会触发 FocusIn。您可以对您的系统进行更多实验。我用过这个tkinter reference

关于python - 条目小部件内容不随函数调用而改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32299106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com