关于tkinter界面的文本更新
我是想先获取数据库checked_time字段所有的数据,然后筛选出不为空的记录并想把它在tkinter界面更新为 检查状态:已检查,可是界面没有更新,一直显示检查状态: ,没有更新为已检查或者未检查,请问为什么?label20 = tk.Label(mw,text = "检查 状态:",font = ("宋体",10)).place(x = 660,y =10)
def checked_status():
global server_name
global user_name
global user_password
global database_name
global datatable_name
global label20
conn = pymysql.connect(server_name, user_name, user_password, database_name, charset="utf8")
cursor = conn.cursor()
sql = "select checked_time from " + datatable_name
cursor.execute(sql)
result = cursor.fetchall()
for i in result:
if i == None:
label20["text"] = "检查状态:未检查"
if len(i) == 0:
label20["text"] = "检查状态:未检查"
else:
label20["text"] = "检查状态:已检查"
conn.commit()
conn.close() 把text换成textvariable,然后用个StringVar试试? 把代码发全看看 Twilight6 发表于 2020-5-16 21:04
把代码发全看看
与这一部分有关的代码全在上面 试试这样:
var1 = StringVar()
var1.set("检查 状态:")
label20 = tk.Label(mw,textvariable=var1,font = ("宋体",10)).place(x = 660,y =10)
def checked_status():
global server_name
global user_name
global user_password
global database_name
global datatable_name
global label20
conn = pymysql.connect(server_name, user_name, user_password, database_name, charset="utf8")
cursor = conn.cursor()
sql = "select checked_time from " + datatable_name
cursor.execute(sql)
result = cursor.fetchall()
for i in result:
if i == None:
var1.set("检查状态:未检查")
if len(i) == 0:
var1.set("检查状态:未检查")
else:
var1.set("检查状态:已检查") txxcat 发表于 2020-5-16 21:41
试试这样:
谢谢大佬,不过还是没用{:9_234:} 悠龠111 发表于 2020-5-16 21:56
谢谢大佬,不过还是没用
写了一段刷新label的测试代码,参考一下。
import tkinter as tk
on_hit = False
def test():
global on_hit
if on_hit == False:
on_hit = True
var1.set("检查状态:未检查")
else:
on_hit = False
var1.set("检查状态:已检查")
mw = tk.Tk()
var1 = tk.StringVar()
var1.set("检查 状态:")
label20 = tk.Label(mw,textvariable=var1,font = ("宋体",10))
b = tk.Button(mw, text="点我", font=("宋体",10), width=10, height=1, command=test)
label20.pack()
b.pack()
mw.mainloop()
页:
[1]