简体   繁体   中英

tkinter change frames with raspberry pi GPIO

Situation: I have tkinter program that has a bunch of frames with various labels and buttons on them. I want changing between them to be controlled by a 12 position switch via GPIO.

Problem: I can't figure out how to get the frames to change, or even to show up for that matter.

I've Tried: I read through this post: Switch between two frames in tkinter but I couldn't figure out how to adapt the code to my purpose.

For now I'm just manually assigning 'HIGH' or 'LOW' values to switch(sw) variables for testing and because I haven't wired the 12 position switch yet. I want to run though the sw variables and if there is a change bring the corresponding frame to the front. The data on the frames is stored so I don't mind destroying the frames that aren't being used.

My Code:

from tkinter import *

# functions
def raise_frame(frame):
    frame.tkraise()

def switch_IO():
    for page, position in zip(pages, sw_positions):
        if position == "HIGH":
            raise_frame(page)
            print('frame: %s is %s' %(page, position))
        else:
            page.destroy()
            print('frame: %s is %s' %(page, position))
    root.after(18000, switch_IO)
    print("*****RESTART******")
# variables
sw01 = "LOW"
sw02 = "LOW"
sw03 = "HIGH"
sw04 = "LOW"
sw05 = "LOW"
sw06 = "LOW"

# start tkloop
root = Tk()
root.geometry('1024x600')

f1_env = Frame(root, bg="red")
f2_fc = Frame(root, bg="blue")
f3_alert = Frame(root, bg="green")
f4_hl = Frame(root, bg="yellow")
f5_news = Frame(root, bg="purple")
f6_sys = Frame(root, bg="teal")
f7_set = Frame(root, bg="magenta")

f1_env.grid(row=0, column=0, sticky=N+S+E+W)
f2_fc.grid(row=0, column=0, sticky=N+S+E+W)
f3_alert.grid(row=0, column=0, sticky=N+S+E+W)
f4_hl.grid(row=0, column=0, sticky=N+S+E+W)
f6_sys.grid(row=0, column=0, sticky=N+S+E+W)
f7_set.grid(row=0, column=0, sticky=N+S+E+W)

# lists
sw_positions = [sw01, sw02, sw03, sw04, sw05, sw06]
pages = [f1_env, f2_fc, f3_alert, f4_hl, f5_news, f6_sys]

# function calls
raise_frame(f1_env)
switch_IO()
root.mainloop()

I think I got it working. I'm a little concerned about a memory leak but I will have to do some long term testing to determine that.

def switch_IO():
    for page, position in zip(pages, sw_positions):
        if position == "HIGH":
            page.tkraise()
            print('frame: %s is %s' %(page, position))
    root.after(1800, switch_IO)
    print("*****RESTART******")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM