如何在tkinter中为标签设置新值? 我有这个代码,但它只显示一个空白窗口 对不起我的英语,如果可以的话,请解释一下。 (我是编程新手) 谢谢你的关注!
import os, time, tkinter, urllib
from tkinter import *
from urllib.request import *
from urllib.error import *
class App:
def __init__(self, master):
v = StringVar()
start_time = time.time()
frame = Frame(master)
frame.pack()
self.label = Label(frame, textvariable=v).pack()
if self.internet_on() == 1:
v.set = "Internet is online"
start_time = time.time()
else:
v.set = "Internet is offline! Downtime: " + str(time.time() - start_time)
def internet_on(self):
try:
response=urllib.request.urlopen('http://www.google.com.br',timeout=1)
return True
except urllib.error.URLError as err: pass
return False
top = tkinter.Tk()
app = App(top)
top.mainloop()
答案 0 :(得分:3)
v.set
是一种方法,你不应该把它写成 -
v.set = "Internet is online"
你应该调用它并传递你想要写为参数的字符串 -
v.set("Internet is online")
和
v.set("Internet is offline! Downtime: " + str(time.time() - start_time))
如评论中所述,您还可以直接设置标签的文本值,然后使用label.config()
更改文本。示例 -
import os, time, tkinter, urllib
from tkinter import *
from urllib.request import *
from urllib.error import *
class App:
def __init__(self, master):
start_time = time.time()
frame = Frame(master)
frame.pack()
if self.internet_on() == 1:
self.label = Label(frame, text="Internet is online")
self.label.pack()
start_time = time.time()
else:
self.label = Label(frame, text="Internet is offline! Downtime: " + str(time.time() - start_time))
self.label.pack()
def internet_on(self):
try:
response=urllib.request.urlopen('http://www.google.com.br',timeout=1)
return True
except urllib.error.URLError as err: pass
return False
top = tkinter.Tk()
app = App(top)
top.mainloop()
解决您的要求的最佳方式 -
在'internet_on'中,定期检查互联网状态(更改标签中的文字)。我已经尝试使用而没有成功。
使用.after()
方法Tk()
指定时间(以毫秒为单位)(可能大约5000毫秒左右,您可以决定这一点)并将iternet_on方法指定为第二个参数然后使用internet_on
方法更新标签。示例 -
import os, time, tkinter, urllib
from tkinter import *
from urllib.request import *
from urllib.error import *
class App:
def __init__(self, master):
start_time = time.time()
self.master = master
self.frame = Frame(master)
self.frame.pack()
self.label = Label(self.frame, text="")
self.label.pack()
self.start_time = time.time()
def internet_on(self):
try:
response=urllib.request.urlopen('http://www.google.com.br',timeout=1)
self.label.config(text="Internet is online")
except urllib.error.URLError as err:
self.label.config(text="Internet is offline! Downtime: " + str(time.time() - self.start_time))
self.master.after(5000,self.internet_on)
top = tkinter.Tk()
app = App(top)
top.after(5000,app.internet_on)
top.mainloop()
答案 1 :(得分:0)
最简单的方法是在忘记返回布尔值并使用root.after
的方法中进行更新:
from tkinter import *
import urllib.request
import urllib.error
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.start_time = time.time()
self.label = Label(root, text="Starting.....")
self.label.pack()
self.internet_on()
def internet_on(self):
try:
urllib.request.urlopen('http://www.google.com.br', timeout=1)
self.label.config(text="Internet is online")
self.start_time = time.time()
except urllib.error.URLError:
self.label.config(text="Internet is offline! Downtime: " + str(time.time() - self.start_time))
root.after(1000, self.internet_on)
root = tkinter.Tk()
app = App(root)
app.pack()