mardi 23 février 2021

how to get the layers of if statements and variables in tkinter correct

I am trying to port a little program for a raspberry pi from hard buttons to a gui (very new at this) and I can't find an easy to understand guide of how the layering of my functions and variable work. It appears that my variable that worked in a non gui program is not propagating out to the rest of the program (if that makes sense.). My if statements also seem to have stopped working. I'm far less interested in getting this solved and more interested in if someone can point me to an understandable methodolgy for getting this into my head correctly. With the code below I want the 'counts' variable to update in Countlabel as it goes up and down (That is problem 1). I also want the if statements in the tensions and drive function to work, but I think I have them at the wrong level (Problem2.)

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

Aucun commentaire:

Enregistrer un commentaire