-1

I use following code:

Switch between two frames in tkinter

import RPi.GPIO as GPIO
from threading import Timer,Thread,Event

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3
#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2

#----------------------------------------------------------
#Setup GPIO
#----------------------------------------------------------
GPIO.setwarnings(False)
# RPi.GPIO Layout verwenden (wie Pin-Nummern)
#GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BCM)

# (GPIO 17,27,22,5) auf Input setzen
chan_list1 = [17, 27, 22, 5]
GPIO.setup(chan_list1, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# (GPIO 12,13,23,24) auf Output setzen
chan_list = [12, 13, 23, 24]
GPIO.setup(chan_list, GPIO.OUT)
#----------------------------------------------------------

#--------------------------------------------------------
#Timer
#--------------------------------------------------------
class perpetualTimer():

   def __init__(self,t,hFunction):
      self.t=t
      self.hFunction = hFunction
      self.thread = Timer(self.t,self.handle_function)

   def handle_function(self):
      self.hFunction()
      self.thread = Timer(self.t,self.handle_function)
      self.thread.start()

   def start(self):
      self.thread.start()

   def cancel(self):
      self.thread.cancel()
#--------------------------------------------------------



class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.overrideredirect(1)              #Remove Title bar  
        self.geometry("480x272")              #Set the window dimensions

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()



def printer():
    print ('PIT(!)')
    SampleApp.show_frame('PageOne') 
if __name__ == "__main__":
    t = perpetualTimer(0.01,printer)
    t.start()

    app = SampleApp()
    app.mainloop()

Is it possible to switch the frames without using buttons? e.g. comparing a counter. if the counter value is 1 show page 1 if counter value is 2 show page 2 and so on.

If I'm honest, I do not remember exactly what I've all tried. One thing I tried was "controller.show_frame("StartPage")" from def printer sub. What I want to achieve is to switch the pages from the "def printer" sub, which is called periodically. I do not know if that's possible?

  • If a button can call a command, you can call it too. Have you tried calling it? – Bryan Oakley Dec 31 '18 at 15:36
  • Yes I have tried it, but no success. That is my problem, that I dont know how to call the e.g. show_frame("PageOne") from my timer event. – hangloose99 Dec 31 '18 at 16:53
  • Please show what you tried, and show what "no success" means. Though, calling any tkinter code from a separate thread will likely fail, if that's where you're doing it. – Bryan Oakley Dec 31 '18 at 17:19
  • Added info in the main thread. btw. Happy New Year :-) – hangloose99 Dec 31 '18 at 18:37
  • The command to switch pages is just a normal method. It's unclear why you're having a problem calling it. There's absolutely nothing special about that method that would prevent you from calling it. If we can't see how you're calling it, we can't say what's wrong. – Bryan Oakley Dec 31 '18 at 18:52
  • SampleApp.show_frame('PageOne') is what I tried – hangloose99 Dec 31 '18 at 19:15

1 Answers1

0

One thing I tried was controller.show_frame("StartPage") from def printer sub.

You have to call switch_frame on the object that has that method. This is no different than any other object or any other class in python.

In your case, since SampleApp is the class that has the method, and app is a global variable that holds an instance of the class, you would do it this way:

app.show_frame("StartPage")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685