2017-04-05 7 views
0

特定のイベントからの時間を測定する簡単なGUIプログラムを作成しています。私はエラーを取得しています:TypeError:暗黙的に 'DoubleVar'オブジェクトをstrに変換できません

Label(self, text=measurement + ": " + str(self.counters[measurement]), font='Verdana 8').grid(row=i+1, column=0, sticky=W) 

...しかし、それは私にこの与える::

/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

私はこれを試してみました。ここ

GUI program

は私がやっていることです。

# 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() 

答えて

1

ラベルはtext引数を使用してテキストを表示できます。または、textvariable引数を使用してDoubleVarのようなtkinter変数の値を表示できます。両方を行うことはできません。

lbl = Label(self, text=measurement + ": ", font='Verdana 8') 
lbl.grid(row=i+1, column=0, sticky=W) 

またはこの:

lbl = Label(self, textvariable=self.counters[measurement], font='Verdana 8') 
lbl.grid(row=i+1, column=0, sticky=W) 

編集:あなたの問題を解決するには、両方を扱うことができるラベルの独自の種類を作るために次のようになります。

import tkinter as tk 
from datetime import datetime 

class FormatLabel(tk.Label): 
    '''A new type of Label widget that allows both a text and 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' ] 

     for i, measurement in enumerate(measurements): 
      self.counters[measurement] = tk.DoubleVar() 
      lbl = FormatLabel(self, 
       text=measurement + ": {:.2f}", # set the 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() 

注これを使用しますか私はあなたのコードの他の部分を修正しました。特に、ワ​​イルドカードのインポート(from module import *)を使用しないでください。ウィジェットを定義するのと同じ行にウィジェットをレイアウトするべきではなく、コードを繰り返さないようにしてください。

+0

それから私はラベルの間に大きなギャップを得るだろう、それが私が1つのラベルにこれらの2つを組み合わせようとした理由です。さあ、私の問題に対する解決策は非常に簡単でなければならない。 –

+0

シンプル、いいえ。しかし、実行可能です。私はあなたに一つの方法を示すために私の答えを編集しました。 – Novel

+0

ありがとうございます、それは精神です! –

関連する問題