我正在创建一个简单的GUI程序来测量某个事件的时间。我得到一个错误:
/usr/bin/python3.5 /home/cali/PycharmProjects/str8/str8.py Traceback (most recent call last): File "/home/cali/PycharmProjects/str8/str8.py", line 51, in app = App(root) File "/home/cali/PycharmProjects/str8/str8.py", line 22, in init Label(self, text=measurement + ": " + self.counters[measurement], font='Verdana 8').grid(row=i+1, column=0, sticky=W) TypeError: Can't convert 'DoubleVar' object to str implicitly
Process finished with exit code 1
我试过这个:
Label(self, text=measurement + ": " + str(self.counters[measurement]), font='Verdana 8').grid(row=i+1, column=0, sticky=W)
…但它给了我这个:
以下是我所做的:
# str8.py
# Program to count time from a certain event
from tkinter import *
from datetime import *
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.grid(sticky = N + W + E + S)
Label(self, text = 'You have been STR8 for:', font="Verdana 8 bold").grid(row=0, sticky=W)
self.counters = dict()
measurements = [ 'Years', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds' ]
for i, measurement in enumerate(measurements):
self.counters[measurement] = DoubleVar()
Label(self, text=measurement + ": " + self.counters[measurement], font='Verdana 8').grid(row=i+1, column=0, sticky=W)
self.counters[measurement].set(0)
Button(self,
text="EXIT",
font="Verdana 8",
height=1,
width=19,
command=quit).grid(row=7, column=0)
self.increment()
def increment(self):
event = datetime(2017, 4, 4, 0, 0, 0)
today = datetime.now()
str8 = today - event
self.counters['Seconds'].set(round(str8.total_seconds(), 2))
self.counters['Minutes'].set(round(str8.total_seconds()/ 60, 2))
self.counters['Hours'].set(round(str8.total_seconds() / 3600, 2))
self.counters['Days'].set(round(str8.total_seconds() / (3600 * 24), 2))
self.counters['Weeks'].set(round(str8.total_seconds() / (3600 * 24 * 7), 2))
self.counters['Years'].set(round(str8.total_seconds() / (3600 * 24 * 7 * 52), 2))
self.after(1000, self.increment)
if __name__ == '__main__':
root = Tk()
app = App(root)
root.title("STR8")
root.resizable(width=False, height=False)
app.mainloop()
标签可以使用
text
参数显示一些文本,也可以使用textvariable
参数显示来自tkinter变量的值,就像DoubleVar一样。它不能两者兼而有之。使用以下选项之一:或者这个:
编辑:解决问题的方法是制作自己的标签类型,可以同时处理以下两个方面:
注意,我还修复了代码的一些其他部分;最值得注意的是,不应使用通配符导入(
from module import *
),不应在定义小部件时将其布局在同一行上,并且应尽量避免重复代码。你知道吗相关问题 更多 >
编程相关推荐