我觸發了回呼函式,讀取編碼器的輸出。
使用GPIO.add_event_detect,所以global counter有當前值,但我不能像使用listbox.insert(END,counter)那樣把它傳遞給tkinter。
import RPi.GPIO as GPIO
from tkinter import ttk
import tkinter as tk
counter = 0
# GPIO引腳 #
A =17
B =27
#-----------#
# GPIO設定
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
#
GPIO.setup(A, GPIO.IN)
GPIO.setup(B, GPIO.IN)
def Enc(A)。
global counter
sleep(0.002)
input_A = GPIO.input(A)
input_B = GPIO.input(B)
if (input_A == 1) and (input_B == 0) 。
counter = 1 print (counter)
#listbox.insert(END,counter)
elif (input_A == 1) and (input_B == 1) 。
counter -= 1 print (counter)
#listbox.insert(END,counter)。
else:
return::
# GPIO輸出檢測: return.
GPIO.add_event_detect(A, GPIO.RISING, callback=Enc, bouncetime=10)
# window settings 視窗設定
window = tk.Tk()
window.title("encoder_value")
# string value?
window.Enc_counter_str = tk.StringVar(value=counter)
# label
tk.Label(window, text='Enc:',fg = "中等紫紅色", bg = "淺灰色", font = "Helvetica 16 bold italic") 。 grid( column=0, row=0, **padding)
# 輸出標籤
window.Enc_label = tk.Label(window)
window.Enc_label.grid( column=0, row=2, columnspan=1, **padding)
window.Enc_label.config(text=window.Enc_counter_str.get(),fg = "中等紫紅色", bg = "淺灰色", font = "Helvetica 16 bold italic")
# tk視窗回圈
window.mainloop()
uj5u.com熱心網友回復:
只要在你的Enc函式中使用window.Enc_label["text"] = str(counter)。
uj5u.com熱心網友回復:
所以從我讀完你的問題后的理解來看,你似乎想在計數器的值每次變化時更新window.Enc_label的值。
這里的問題在于這一行--:
window.Enc_counter_str = tk.StringVar(value=counter)
在這里,當你把字串var的值設定為counter時,你只是分配了counter的 "那個時間 "的值(假設執行這一行時counter是1,所以字串var的值被分配為1,而不是實際變數的參考。)
但這是可行的,因為你確實希望初始值也是一樣的。 另一個問題是,你沒有更新字串var的值,因為你假設字串var存盤了計數器的參考,但它沒有。
所以每次你修改計數器的值時,你可以這樣做--:
def Enc(A)。
global counter
sleep(0.002)
input_A = GPIO.input(A)
input_B = GPIO.input(B)
if (input_A == 1) and (input_B == 0) 。
counter = 1 print (counter)
elif (input_A == 1) and (input_B == 1) 。
counter -= 1 print (counter)
else:
return
window.Enc_counter_str.set(counter)
另外,現在你希望Label物件立即意識到字串變數是它的取值來源,所以它定期檢查它的更新,你可以通過傳遞textvariable引數來實作 -:
window.Enc_label = tk.Label(window, textvariable = window.Enc_counter_str)
還有另一種方法,雖然不那么整潔,但也是一種可能的作業方案,就是在每次更新計數器時運行這一行 -:
window.Enc_label.config(text=window.Enc_counter_str。 get(),fg = "中等紫紅色", bg = "淺灰色", font = "Helvetica 16 bold italic")
目前這一行你只是在Label物件的初始化時運行,因此文本不會被更新。
但是通過我們的第一個方法,這一行可以縮短為-:
window.Enc_label.config(fg = " medium violet red", bg = "light gray", font = "Helvetica 16 bold italic")
現在還要確保將你的標簽宣告放在函式之前,這樣python就已經知道你所參考的字串var了。
最后的代碼變成了 -:
import RPi.GPIO as GPIO
from tkinter import ttk
import tkinter as tk
counter = 0
# GPIO 引腳 #
A =17
B =27
#-----------#
# GPIO設定
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
#
GPIO.setup(A, GPIO.IN)
GPIO.setup(B, GPIO.IN)
# 視窗設定 # 視窗設定
window = tk.Tk()
window.title("encoder_value")
# string value?
window.Enc_counter_str = tk.StringVar(value=counter)
# label
tk.Label(window, text='Enc:',fg = "中等紫紅色", bg = "淺灰色", font = "Helvetica 16 bold italic") 。 grid( column=0, row=0, **padding)
# 輸出標籤
window.Enc_label = tk.Label(window, textvariable = window.Enc_counter_str) # CHANGE 1
window.Enc_label.grid( column=0, row=2, columnspan=1, **padding)
window.Enc_label.config(fg = "中等紫紅色", bg = "淺灰色", font = "Helvetica 16 bold italic") # CHANGE 2[/span
def Enc(A)。
global counter
sleep(0.002)
input_A = GPIO.input(A)
input_B = GPIO.input(B)
if (input_A == 1) and (input_B == 0) 。
counter = 1 print (counter)
elif (input_A == 1) and (input_B == 1) 。
counter -= 1 print (counter)
else:
window.Enc_counter_str.set(counter) # CHANGE 3
# GPIO輸出檢測 # GPIO.add_event?
GPIO.add_event_detect(A, GPIO.RISING, callback=Enc, bouncetime=10)
# tk 視窗回圈
window.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330580.html
標籤:
