簡體   English   中英

更新線程的tkinter gui

[英]update threaded tkinter gui

我的pi連接了一個小顯示器。 現在,我有了一個Python腳本,可以測量兩次gpio標頭事件之間的時間。 我想顯示這個時間(獲得這個時間的腳本運行正常)。 為此,我創建了一個tkinter窗口。 在那里,我有一個標簽應該在這次顯示。 我已經對gui函數進行了線程化處理,以使程序仍然可以監聽GPIO引腳。

def guiFunc():
    gui = Tk()
    gui.title("Test")
    gui.geometry("500x200")
    app = Frame(gui)
    app.grid()
    beattime = Label(app, text = "test")
    beattime.grid()
    gui.mainloop()


gui_thread = threading.Thread(target = guiFunc)
gui_thread.start()


while True:
    time.sleep(.01)
    if (GPIO.input(3)):
        time = trigger()  #trigger is the function to trigger the 'stopwatch'
        global beattime
        beattime['text'] = str(time)
        while GPIO.input(3): #'wait' for btn to release (is there a better way?)
            print "btn_pressed"

因此,由於添加了以下幾行,因此該程序沒有執行任何操作:

global beattime
beattime['text'] = str(time)

我究竟做錯了什么?

使用tkinter.StringVar

# omitting lines
global timevar
timevar = StringVar()
timevar.set("Test")
beattime = Label(app, textvariable=timevar)
# omitting lines

#changing the text:
while True:
    time.sleep(.01)
    if (GPIO.input(3)):
       time = trigger()  #trigger is the    function to trigger the 'stopwatch'
       timevar.set(str(time))
       root.update() #just in case

       while GPIO.input(3): #'wait' for btn to release (is there a better way?)
          print "btn_pressed"

並且您應該在主線程中運行gui。 不建議從不同線程調用gui調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM