2017-05-30 71 views
1

我剛寫的程序是我的操場找出Tkinter如何工作。
我的問題是如何顯示我的變量「timelabel」作爲標籤。Python的顯示變量與tkinter內的方法

我已經制作了一個名爲timerefresher的標籤,但它沒有出現。

我知道DigitalClock類不能有效寫入。我是新來的 ;)。

def Clockupdate(time): 
    timelabel = time 
    timerefresher = Label(root, textvariable=timelabel) 
    timerefresher.pack() 
    #print(timelabel) To test if variable made it this far. 

class DigitalClock: 
    def secondrefesher(self): 
     newtime = "" 
     while 1 == 1: 
      oldtime = datetime.datetime.now() 
      a = str(oldtime.hour) 
      b = str(oldtime.minute) 
      c = str(oldtime.second) 
      curtime = (a+":"+b+':'+c) 
      if curtime != newtime: 
       newtime = curtime 
       #print("made it here") 
       Clockupdate(newtime) 
      else: 
       time.sleep(0.20) 

DC = DigitalClock() 

root = Tk() 
mainlabel = Label(root, text="Hey this is working!") 
Pressme = Button(root, text="Press me!", command=Presstoshowtextandpic, 
bg='red', fg='white') 
Clock = Button(root, text="Clock?", command=DC.secondrefesher, bg='blue', 
fg='white') 
Photo = PhotoImage(file="test.png") 
ShowPhoto = Label(root, image=Photo) 

mainlabel.pack() 
Pressme.pack() 
Clock.pack() 
root.mainloop() 
+0

請提供您的進口,以便我們可以查看您是否擁有您需要的所有進口產品。 –

回答

1

好了,所以這是我將與你的代碼做來完成你正在嘗試做的。

首先讓我們確保我們有正確的進口。

from tkinter import * 
import datetime 
import time 

我創造了這個doNothing()功能作爲我不是目前正在測試的任何命令的佔位符。

def doNothing(): 
    pass 

現在的方式,你有你Clockupdate(time):功能會導致標籤被你點擊時鐘按鈕每次增加。所以這可能不是你想要的,因爲它只是添加新的標籤而不是替換現有的標籤。看看這個功能。我所做的全部是.config()文本作爲函數的時間參數。請注意,您不需要重新定義時間標籤或類別的任何時間,只需使用時間作爲您的可用空間。

def Clockupdate(time): 
    timerefresher.config(text = time) 

,如果你想使用一類特定原因,但我認爲你應該只使用一個函數在這裏我不知道。也儘量保持你的報價一致。我知道它們是可以互換的,但它是保持它們一致的良好實踐。

def secondrefesher(): 
    newtime = "" 
    oldtime = datetime.datetime.now() 
    a = str(oldtime.hour) 
    b = str(oldtime.minute) 
    c = str(oldtime.second) 
    curtime = (a + ":" + b + ":" + c) 
    if curtime != newtime: 
     newtime = curtime 
     Clockupdate(newtime) 

注意我始終把命令在按鈕的任何配置的結束。它有助於管理代碼,因此您可以在滾動代碼時檢查每行的結尾。我也改變了Pressme的命令,所以我可以測試代碼。您沒有提供Presstoshowtextandpic的代碼,也無法用該部分測試代碼。請記住在這裏提問時要使用MCVE

root = Tk() 
mainlabel = Label(root, text="Hey this is working!") 
Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command = doNothing) 
Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher) 
Photo = PhotoImage(file = "test.png") 
ShowPhoto = Label(root, image = Photo) 

在這裏,我創建了一次的時間標籤,然後你可以調用更新函數,所有你想改變文本到當前時間。

timerefresher = Label(root, text = "") 
timerefresher.pack() 

mainlabel.pack() 
Pressme.pack() 
Clock.pack() 
root.mainloop() 

這是我想完成的代碼看起來應該像你。

from tkinter import * 
import datetime 
import time 

def Clockupdate(time): 
    timerefresher.config(text = time) 

def secondrefesher(): 
    newtime = "" 
    oldtime = datetime.datetime.now() 
    a = str(oldtime.hour) 
    b = str(oldtime.minute) 
    c = str(oldtime.second) 
    curtime = (a + ":" + b + ":" + c) 
    if curtime != newtime: 
     newtime = curtime 
     Clockupdate(newtime) 

root = Tk() 
mainlabel = Label(root, text = "Hey this is working!") 
Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command = Presstoshowtextandpic) 
Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher) 
Photo = PhotoImage(file = "test.png") 
ShowPhoto = Label(root, image = Photo) 

timerefresher = Label(root, text = "") 
timerefresher.pack() 

mainlabel.pack() 
Pressme.pack() 
Clock.pack() 
root.mainloop() 

編輯:

如果你要創建一個時鐘,始終是積極的,不需要你點擊就可以使用.after()按鈕。

看看這個例子。

from tkinter import * 
import datetime 
import time 

root = Tk() 
mainlabel = Label(root, text = "Hey this is working!") 
Photo = PhotoImage(file = "test.png") 

timerefresher = Label(root, text = "") 
timerefresher.pack() 

status_time = "" 
def tick(): 
    global status_time 
    time2 = time.strftime("%H:%M:%S") 
    if time2 != status_time: 
     status_time = time2 
     timerefresher.config(text = time2) 
    timerefresher.after(200, tick) 
tick() 

mainlabel.pack() 
root.mainloop() 
+0

非常好!你解決了我所有的問題。特別是'.after()'非常有用!你建議使用'.after()'而不是'time.sleep()來修補任何暫停?' –

+0

@Anton:tkinter中的'.sleep()'會暫停整個程序,而不是等待執行下一個操作,'.after()'會計算滴答,然後執行任務。看看這篇文章的更詳細的[睡眠](https://stackoverflow.com/questions/30284270/why-does-time-sleep-pause-tkinter-window-before-it-opens)的睡眠問題。 –

0

你應該寫一個MCVE這樣一方面可以測試你的代碼,尤其是東西不見了(如Presstoshowtextandpic())。

當閱讀你的代碼的其餘部分,我認爲你需要閱讀有關variable classes和修改Clockupdate()如下:

def Clockupdate(time): 
    timelabel = StringVar() # modified 
    timelabel.set(time) # added 
    timerefresher = Label(root, textvariable=timelabel.get()) #modified 
    timerefresher.pack() 

附:你應該寫clock_update()而不是Clockupdate()

+0

我改變了你的建議,變量propably已被正確分配,但我認爲問題是在timerefresher.pack線。我寫了一個MCVE。由於某種原因它只是不更新​​屏幕。 –

+0

[鏈接] https://ufile.io/6e99w –