[英]how to get the layers of if statements and variables in tkinter correct
我正在嘗試將樹莓派的一個小程序從硬按鈕移植到 gui(這方面非常新),但我找不到一個易於理解的關於我的函數分層和變量如何工作的指南。 看來我在非 gui 程序中工作的變量沒有傳播到程序的 rest (如果有意義的話)。 我的 if 語句似乎也停止了工作。 我對解決這個問題的興趣要小得多,而更感興趣的是是否有人可以為我指出一種可以理解的方法來正確地將其帶入我的腦海。 使用下面的代碼,我希望 Countlabel 中的“計數”變量隨着它的上升和下降而更新(這是問題 1)。 我還希望緊張局勢中的 if 語句並驅動 function 工作,但我認為我將它們置於錯誤的級別(問題 2。)
import tkinter as tk
import tkinter.font
from gpiozero import LED, Button
from signal import pause
import math
import busio
import board
import adafruit_mcp4725
i2c = busio.I2C(board.SCL, board.SDA)
dac = adafruit_mcp4725.MCP4725(i2c, address = 0x63)
win=tk.Tk()
win.title("tkinter test")
myFont=tkinter.font.Font(family = 'Helvetica', size = 12, weight = 'bold')
hall=Button(21)
light1=LED(26) ##indicate you are in counting mode
light2=LED(13) ##indicate you are in tension mode
light3=LED(19)
counts = 0
def spins():
global counts
counts += 1
print("out", counts)
def reels():
global counts
counts -= 1
print("in", counts)
def home():
hall.when_pressed = spins
light1.on()
light2.off()
light3.off()
dac.normalized_value = 0.0
def tension():
hall.when_pressed = reels
light1.off()
light2.on()
light3.off()
if counts > 5:
dac.normalized_value = 0.25
else:
dac.normalized_value = 0.0
print("in", counts)
def drive():
hall.when_pressed = reels
light1.off()
light2.off()
light3.on()
if counts > 5:
dac.normalized_value = 1.0
else:
dac.normalized_value = 0.0
print("in", counts)
def exitProgram():
win.quit()
homeButton=tk.Button(win, text='Home', font=myFont, command=home, bg='cyan', height=1, width=24)
homeButton.grid(row=0, column=0, sticky=tk.NSEW)
tensionButton=tk.Button(win, text='Tension', font=myFont, command=tension, bg='cyan', height=1, width=24)
tensionButton.grid(row=1, column=0, sticky=tk.NSEW)
driveButton=tk.Button(win, text='Drive', font=myFont, command=drive, bg='cyan', height=1, width=24)
driveButton.grid(row=2, column=0, sticky=tk.NSEW)
exitButton=tk.Button(win, text='Exit', font=myFont, command=exitProgram, bg='cyan', height=1, width=6)
exitButton.grid(row=4, sticky=tk.E)
Countlabel=tk.Label(win, text=counts)
Countlabel.grid(row=5, sticky=tk.NSEW)enter code here
我的 if 語句處於錯誤的級別。 當我將它們移至正在計數的 function 時,一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.