i creating simple gui program measure time event. getting error:
/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 str implicitly
process finished exit code 1
i have tried this:
label(self, text=measurement + ": " + str(self.counters[measurement]), font='verdana 8').grid(row=i+1, column=0, sticky=w)
...but gives me this:
here have done:
# str8.py # program count time event tkinter import * 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' ] 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()
a label can display text using text
argument, or can display value tkinter variable doublevar using textvariable
argument. cannot both. either use this:
lbl = label(self, text=measurement + ": ", font='verdana 8') lbl.grid(row=i+1, column=0, sticky=w)
or this:
lbl = label(self, textvariable=self.counters[measurement], font='verdana 8') lbl.grid(row=i+1, column=0, sticky=w)
edit: solution problem make own type of label can handle both:
import tkinter tk datetime import datetime class formatlabel(tk.label): '''a new type of label widget allows both text , textvariable''' def __init__(self, master=none, **kwargs): self.textvariable = kwargs.pop('textvariable', tk.stringvar(master)) self.text = kwargs.pop('text', '{}') self.textvariable.trace('w', self.update_text) tk.label.__init__(self, master, **kwargs) def update_text(self, *args): self.config(text=self.text.format(self.textvariable.get())) class app(tk.frame): def __init__(self, master=none, **kwargs): tk.frame.__init__(self, master, **kwargs) lbl = tk.label(self, text = 'you have been str8 for:', font="verdana 8 bold") lbl.grid(row=0, sticky=tk.w) self.counters = dict() measurements = [ 'years', 'weeks', 'days', 'hours', 'minutes', 'seconds' ] i, measurement in enumerate(measurements): self.counters[measurement] = tk.doublevar() lbl = formatlabel(self, text=measurement + ": {:.2f}", # set rounding here textvariable = self.counters[measurement], font='verdana 8') lbl.grid(row=i+1, column=0, sticky=tk.w) self.counters[measurement].set(0) btn = tk.button(self, text="exit", font="verdana 8", height=1, width=19, command=quit) btn.grid(row=7, column=0) self.increment() def increment(self): event = datetime(2017, 4, 4, 0, 0, 0) today = datetime.now() str8 = (today - event).total_seconds() self.counters['seconds'].set(str8) self.counters['minutes'].set(str8 / 60.) self.counters['hours'].set(str8 / 3600.) self.counters['days'].set(str8 / (3600. * 24)) self.counters['weeks'].set(str8 / (3600. * 24 * 7)) self.counters['years'].set(str8 / (3600. * 24 * 7 * 52)) self.after(1000, self.increment) def main(): root = tk.tk() app = app(root) app.pack() root.title("str8") root.resizable(width=false, height=false) app.mainloop() if __name__ == '__main__': main()
note fixed other parts of code; notably should not use wildcard imports (from module import *
), should not layout widgets on same line define them, , should try not repeat code.
Comments
Post a Comment