Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Multiple frames with tkinter - How to make them run on fullscreen mode
#1
Hello guys!

I wrote a code using tkinter to switch between frames after a few seconds. The problem is: All the application must run in fullscreen.

Here is the code.

from tkinter import *


N_TRIALS = 3
COUNT=-2
N_INTERVALS = 2*N_TRIALS + 1
RELAXING_TIME = 2000
RESTING_TIME = 2000
TASKING_TIME = 2000
start_btn_state = False

def raise_frame(frame):
    frame.tkraise()
    global COUNT
    COUNT+=1
    print(COUNT)

    if (frame == f3 and COUNT==N_INTERVALS):
        frame.after(RESTING_TIME,lambda:raise_frame(f5)) 

    elif(frame == f5):

        frame.after(RELAXING_TIME,lambda:raise_frame(f6)) 

    elif frame == f2: 
                frame.after(RELAXING_TIME, lambda:raise_frame(f3)) 
    elif frame == f3: 
                frame.after(RESTING_TIME, lambda:raise_frame(f4))

    elif frame == f4: 
                 frame.after(TASKING_TIME, lambda:raise_frame(f3))

    elif frame==f6:
        root.destroy()           


def finish():
    root.destroy()

def start():
    start_btn_state=True
    raise_frame(f2)


root = Tk()

f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)
f5 = Frame(root)
f6 = Frame(root)



for frame in (f1, f2, f3, f4, f5,f6):
   frame.grid(row = 0, column = 0, sticky='nsew')




lb0=Label(f1, text="blablabla",font=("Arial Bold",10))
lb0.pack(padx = 10, pady=10)
lb1=Label(f1, text="blablabla",font = ("Arial Bold",10),fg = "black")
lb1.pack(padx = 10, pady=10)
lb2 = Label(f1, text="blablabla",font = ("Arial Bold",10),fg = "green")
lb2.pack(padx = 10, pady=10)
lb3 = Label(f1, text="blablabla",font = ("Arial Bold",10),fg = "blue") 
lb3.pack(padx = 10, pady=10)
lb4 = Label(f1, text="blablabla",font = ("Arial Bold",10),fg = "red")
lb4.pack(padx = 10, pady=10)
# =============================================================================
# Start Button
# =============================================================================
start_value=BooleanVar()
btn_start=Button(f1, text="Start",font = ("Arial Bold",20), command = start)
btn_start.pack(padx=10,pady=30)
###############################################################################
# =============================================================================
# Close Button
# =============================================================================
close_value=BooleanVar()
btn_close=Button(f1, text="CLOSE",font = ("Arial Bold",20), command=finish)
btn_close.pack(padx=10,pady=30)
###############################################################################


# =============================================================================
# Frame 2
# =============================================================================
Label(f2, text='ACTION 1').pack()
#Button(f2, text='Go to frame 3', command=lambda:raise_frame(f1)).pack()
f2.config(bg='red')

###############################################################################

# =============================================================================
# Frame 3
# =============================================================================
Label(f3, text='ACTION2').pack(side='left')
f3.config(bg='green')
###############################################################################

# =============================================================================
# Frame 4
# =============================================================================
Label(f4, text='ACTION3').pack()
f4.config(bg='blue')

###############################################################################

# =============================================================================
# Frame 5
# =============================================================================
Label(f5, text='RELAXING FINAL').pack()
f5.config(bg='black')
###############################################################################


raise_frame(f1)
root.mainloop()
I don't know how to expand the frames to fill all the window. I tried everything: geometry, width and height, etc. It seems probably (100% sure) that I am doing something wrong to put those frames on fullscreen.

could anyone help me?

Sorry about the code that is not in OOP. I am learning about it.

I appreciate the help!

Best Regards
Quote
#2
my suggestion (you won't like it) is to use Qt or wxpython (pheonix)
The difficulty I always ran into with tkinter geometry is the reason why I stopped using it.
It's a snap with wxpython, including docking, resizing, everything!

see (for example): https://python-forum.io/Thread-Perfectly...n-template
Quote
#3
Hi eabs86
Here one possible solution. Just experimental not optimised:
import tkinter as tk

'''
My script is programmed in OOP. I replaced the frames as follows:
frame1 = self.pages[0].frame
frame2 = self.pages[1].frame
frame3 = self.pages[2].frame
frame4 = self.pages[3].frame
frame5 = self.pages[4].frame
frame6 = self.pages[5].frame
'''

APP_TITLE = "Frame Stack"
MIN_APP_WIDTH = 400
MIN_APP_HEIGHT = 400


class AppPage(tk.Frame):
    
    def __init__(self, app, page_nr, **kwargs):
        self.app = app
        self.page_nr = page_nr
        
        tk.Frame.__init__(self, app, **kwargs)
        self.frame = tk.Frame(self)
        self.frame.pack(fill='both', expand=True, padx=0, pady=0)        

            
class Application(tk.Frame):
    NUM_OF_PAGES = 6
    N_TRIALS = 3
    COUNT=-2
    N_INTERVALS = 2 * N_TRIALS + 1
    RELAXING_TIME = 2000
    RESTING_TIME = 2000
    TASKING_TIME = 2000
    LABEL_FONT = ("Arial Bold",10)
    
    def __init__(self, app_win):
        self.app_win = app_win
        tk.Frame.__init__(self, app_win)
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.pages = list()
                  
        for page_ptr in range(self.NUM_OF_PAGES):
            page = self.create_page_frame(page_ptr)
            if page_ptr == 1:
                page.frame.config(bg='red')
                tk.Label(page.frame, text='ACTION-1').pack()
            elif page_ptr == 2:
                page.frame.config(bg='green')
                tk.Label(page.frame, text='ACTION-2').pack(side='left')
            elif page_ptr == 3:
                page.frame.config(bg='blue')
                tk.Label(page.frame, text='ACTION-3').pack()
            elif page_ptr == 4:
                page.frame.config(bg='black')
                tk.Label(page.frame, text='RELAXING FINAL').pack()
                
        self.build_start_page()
        self.show_page(0)
    
    def build_start_page(self):
        container = self.pages[0].frame

        tk.Label(container, text="blablabla",font=("Arial Bold",10)
            ).pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "black").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "green").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "blue").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "red").pack(padx = 10, pady=10)

        tk.Button(container, text="Start",font=("Arial Bold",20),
            command=self.start).pack(padx=10,pady=30)

        tk.Button(container, text="CLOSE",font=("Arial Bold",20),
            command=self.finish).pack(padx=10,pady=30)
            
        container.pack_configure(fill='none', expand=True)
        
    def start(self):
        self.raise_page(1)
        
    def finish(self):
        self.app_win.withdraw()
        self.app_win.destroy()
    
    def raise_page(self, page_ptr):
        print(page_ptr)
        self.show_page(page_ptr)
        if page_ptr == 2 and self.COUNT == self.N_INTERVALS:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 4)
        elif page_ptr == 4:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 5)
        elif page_ptr == 1:
            self.app_win.after(self.RELAXING_TIME, self.raise_page, 2)
        elif page_ptr == 2:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 3)
        elif page_ptr == 3:
            self.app_win.after(self.TASKING_TIME, self.raise_page, 4) 
        elif page_ptr == 5:
            self.show_page(0) 
        
    def show_page(self, page_ptr=0):
        self.pages[page_ptr].lift()
               
    def create_page_frame(self, page_ptr):
        page = AppPage(self, page_ptr)
        page.grid(row=0, column=0, sticky='wens')
        self.pages.append(page)
        return page

           
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.minsize(MIN_APP_WIDTH, MIN_APP_HEIGHT)
    
    # With title bar
    #app_win.attributes('-zoomed', True)
    
    # Without title bar
    app_win.attributes('-fullscreen', True)
    
    app = Application(app_win)
    app.pack(fill='both', expand=True)
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()
Greetings wuf Smile
Quote
#4
(Sep-18-2018, 03:45 PM)Larz60+ Wrote: my suggestion (you won't like it) is to use Qt or wxpython (pheonix)
The difficulty I always ran into with tkinter geometry is the reason why I stopped using it.
It's a snap with wxpython, including docking, resizing, everything!

see (for example): https://python-forum.io/Thread-Perfectly...n-template

Humm. I will try later.
Since I don't have much time to implement with another lib, I will still try to implement my application using tkinter.
Thank you for your sugestion!

(Sep-18-2018, 04:28 PM)wuf Wrote: Hi eabs86
Here one possible solution. Just experimental not optimised:
import tkinter as tk

'''
My script is programmed in OOP. I replaced the frames as follows:
frame1 = self.pages[0].frame
frame2 = self.pages[1].frame
frame3 = self.pages[2].frame
frame4 = self.pages[3].frame
frame5 = self.pages[4].frame
frame6 = self.pages[5].frame
'''

APP_TITLE = "Frame Stack"
MIN_APP_WIDTH = 400
MIN_APP_HEIGHT = 400


class AppPage(tk.Frame):
    
    def __init__(self, app, page_nr, **kwargs):
        self.app = app
        self.page_nr = page_nr
        
        tk.Frame.__init__(self, app, **kwargs)
        self.frame = tk.Frame(self)
        self.frame.pack(fill='both', expand=True, padx=0, pady=0)        

            
class Application(tk.Frame):
    NUM_OF_PAGES = 6
    N_TRIALS = 3
    COUNT=-2
    N_INTERVALS = 2 * N_TRIALS + 1
    RELAXING_TIME = 2000
    RESTING_TIME = 2000
    TASKING_TIME = 2000
    LABEL_FONT = ("Arial Bold",10)
    
    def __init__(self, app_win):
        self.app_win = app_win
        tk.Frame.__init__(self, app_win)
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.pages = list()
                  
        for page_ptr in range(self.NUM_OF_PAGES):
            page = self.create_page_frame(page_ptr)
            if page_ptr == 1:
                page.frame.config(bg='red')
                tk.Label(page.frame, text='ACTION-1').pack()
            elif page_ptr == 2:
                page.frame.config(bg='green')
                tk.Label(page.frame, text='ACTION-2').pack(side='left')
            elif page_ptr == 3:
                page.frame.config(bg='blue')
                tk.Label(page.frame, text='ACTION-3').pack()
            elif page_ptr == 4:
                page.frame.config(bg='black')
                tk.Label(page.frame, text='RELAXING FINAL').pack()
                
        self.build_start_page()
        self.show_page(0)
    
    def build_start_page(self):
        container = self.pages[0].frame

        tk.Label(container, text="blablabla",font=("Arial Bold",10)
            ).pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "black").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "green").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "blue").pack(padx = 10, pady=10)
        tk.Label(container, text="blablabla",font = ("Arial Bold",10),
            fg = "red").pack(padx = 10, pady=10)

        tk.Button(container, text="Start",font=("Arial Bold",20),
            command=self.start).pack(padx=10,pady=30)

        tk.Button(container, text="CLOSE",font=("Arial Bold",20),
            command=self.finish).pack(padx=10,pady=30)
            
        container.pack_configure(fill='none', expand=True)
        
    def start(self):
        self.raise_page(1)
        
    def finish(self):
        self.app_win.withdraw()
        self.app_win.destroy()
    
    def raise_page(self, page_ptr):
        print(page_ptr)
        self.show_page(page_ptr)
        if page_ptr == 2 and self.COUNT == self.N_INTERVALS:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 4)
        elif page_ptr == 4:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 5)
        elif page_ptr == 1:
            self.app_win.after(self.RELAXING_TIME, self.raise_page, 2)
        elif page_ptr == 2:
            self.app_win.after(self.RESTING_TIME, self.raise_page, 3)
        elif page_ptr == 3:
            self.app_win.after(self.TASKING_TIME, self.raise_page, 4) 
        elif page_ptr == 5:
            self.show_page(0) 
        
    def show_page(self, page_ptr=0):
        self.pages[page_ptr].lift()
               
    def create_page_frame(self, page_ptr):
        page = AppPage(self, page_ptr)
        page.grid(row=0, column=0, sticky='wens')
        self.pages.append(page)
        return page

           
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.minsize(MIN_APP_WIDTH, MIN_APP_HEIGHT)
    
    # With title bar
    #app_win.attributes('-zoomed', True)
    
    # Without title bar
    app_win.attributes('-fullscreen', True)
    
    app = Application(app_win)
    app.pack(fill='both', expand=True)
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()
Greetings wuf Smile


Woow! Very Good! In OOP it really gets much better and more organized. It's a shame that he is not yet able to codify so hehehe.
I'm learning yet! One day I'll get there!

Thanks for the reply!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGUI] Switching between two frames using Tkinter jenkins43 1 167 Jul-17-2019, 10:53 AM
Last Post: metulburr
  [Tkinter] adapt management grid mode to management pack mode atlass218 4 337 Apr-24-2019, 11:04 AM
Last Post: atlass218
  [Tkinter] switching frames nick123 2 429 Apr-18-2019, 04:50 PM
Last Post: francisco_neves2020
  Tkinter - Make changes to graph and update it adriancovaci 0 453 Apr-08-2019, 09:02 AM
Last Post: adriancovaci
  [Tkinter] Python 3.5 tk fullscreen nuska012 2 1,278 Dec-11-2018, 06:56 AM
Last Post: jfong

Forum Jump:


Users browsing this thread: 1 Guest(s)