2017-07-03 8 views
-1

メッセージを表示する時刻を表示したい。私はそれをしましたが、時間は更新されませんでした。私がメインループを始めた時と同じように残る。私は以下に私のコードを添付しました。私はPythonを学びたいと思っているので、私が得ることのできるどんな形の助けにも感謝します。ありがとう。メッセージのPython Tkinter更新時間

from tkinter import * 
    import time 

    time3 = time.strftime('%H:%M:%S') 

    def tick(): 
     global time1 
     time2 = time.strftime('%H:%M:%S') 
     clock.config(text=time2) 
     clock.after(200, tick) 

    root = Tk() 
    root.title("Test GUI") 
    time1 = ' ' 


    def newfile(): 
     message = (time3 + ':' + "Creating new file..... \n") 
     text.insert(0.0, message) 

    def openfile(): 
     message = (time3 + ':' + "Opening existing file..... \n") 
     text.insert(0.0, message) 

    def starttest(): 
     message = (time3 + ':' + "Start testing \n") 
     text.insert(0.0, message) 

    def stoptest(): 
     message = (time3 + ':' + "Stop testing \n") 
     text.insert(0.0, message) 

    topFrame = Frame(root) 
    topFrame.pack(side = TOP) 
    bottomFrame = Frame(root) 
    bottomFrame.pack(side = BOTTOM) 

    clock = Label(root, font=('times', 10, 'bold'), bd = 1, relief = SUNKEN, anchor = E) 
    but1 = Button(topFrame, text = "START", command = starttest) 
    but2 = Button(topFrame, text = "STOP", command = stoptest) 
    text = Text(topFrame, width = 35, height = 5, wrap = WORD) 

    clock.pack(side = BOTTOM, fill = X) 
    but1.grid(row = 3, column = 3) 
    but2.grid(row = 3, column = 4) 
    text.grid(row = 1, column = 0, columnspan =2, sticky = W) 

    menu = Menu(topFrame) 
    root.config(menu = menu) 

    subMenu = Menu(menu) 
    menu.add_cascade(label = "File", menu = subMenu) 
    subMenu.add_command(label = "New File", command = newfile) 
    subMenu.add_command(label = "Open File", command = openfile) 

    tick() 
    root.mainloop() 
+2

tick' 'とインデントの問題があり、私たちがすることができますそれが終わるところを教えてください。また、あなたの 'tk'オブジェクトに' mainloop'呼び出しがありません。また、 'text'は何ですか?どこにも定義されていません。なぜあなたは 'time1'と' time2'で時々動作していますか、時には 'time3'で動作していますか? – kabanus

+0

研究をしましたか?このサイトには、tkinterを使ったカウントダウンタイマーや時計に関する多くの質問があります。 –

+0

こんにちは、私が今行っているコードをすべて含めておきたいのは残念です。私はいくつかの研究をしましたが、問題はリアルタイムで更新するコードをいくつか追加する必要がありますが、問題はタイム3にあるはずですが、私は固執しています。誰もが私にアイデアを与えてくれてありがとう。もう一度ありがとう。 –

答えて

0

まず、私はあなたがそれをより組織化されるようにクラスとしてアプリケーションを(Best way to structure a tkinter applicationを参照)を書き込むことをお勧めします。

class Timer(tk.Frame): 
    def __init__(self, parent): # create the time frame... 
     self.parent = parent 
     self.time_1 = " " 
     self.time_label = tk.Label(text=self.time_1) 
     self.time_label.pack() 
     self.random_lbl = tk.Label(text="hi lol") 
     self.random_lbl.pack() 
     self.update_clock() # we start the clock here. 

あなたが時間を更新すると、あなたは(たとえば、この場合には、それはだTIME_1)時間のために使用される変数に固執:

def update_clock(self): # update the clock... 
     self.time_2 = time.strftime("%H:%M:%S") 
     self.time_1 = self.time_2 # we are changing time_1 here. 
     self.time_label.config(text=self.time_1) 
     self.parent.after(200, self.update_clock) # then we redo the process. 

root = tk.Tk() # create the root window. 
timer = Timer(root) 
root.mainloop() 
+0

クイックポインタ:タイムスタンプメッセージを変更するには、単に「+」を追加するだけです。「+」スパムハム「...」などがあります。 –

+0

ボタンを押したときの現在の時刻をプリントアウトできるかどうか聞いてみたいと思います。私はあなたが言及した方法を試しましたが、今私は同じ時間を表示し続ける私のディスプレイボックスにタイムアウトを印刷しようとするたびに。 –

関連する問題