Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using buttons in Tkinter to navigate to different pages of the application?

I have quite a simple question here. In Tkinter (python), I was wondering who to use a button to go to different pages of my application, e.g a register page, and a login page. I am aware that GUI does not have 'pages' like websites do, I've seen a few different ways, but what is the best way to make links to different pages?

like image 221
Caspar Wylie Avatar asked Feb 11 '13 17:02

Caspar Wylie


People also ask

Can Tkinter have multiple pages?

Here is a step by step process to create multiple Tkinter Page Frames and link them! This can be used as a boilerplate for more complex python GUI applications like creating interfaces for Virtual Laboratories for experiments, classrooms, etc. Here are the steps: Create three different pages.

How do buttons work in Tkinter?

Introduction to Tkinter button widgetTypically, you use a text or an image to display the action that will be performed when clicked. Buttons can display text in a single font. However, the text can span multiple lines. On top of that, you can make one of the characters underline to mark a keyboard shortcut.

How to navigate between Tkinter frames using Startpage?

The StartPage is simple with two buttons to go to Page 1 and Page 2. Page 1 has two buttons, One for Page 2 and another to return to Start Page. Page 2 also has two buttons, one for Page 1 and others to return to StartPage. This is a simplistic application of navigating between Tkinter frames.

How to go to the next page in Python Tkinter program?

Multiple page application needs an option wherein users can switch or toggle among different pages. So in this tutorial, we will learn how to go to the next page in Python Tkinter Program. When there is a reference to the next page added on a particular widget and the user clicks on that widget then he moves to the next page.

How to initialize a button in a Tkinter application?

A Button can be initialized using the for loop in a Tkinter application. Let us suppose that we want to create multiple buttons, each with different commands or operations defined in it. We have to first initialize the Button inside a for loop. The iterator will return the object for which multiple instances of the button will be created.

How many classes are there In Tkinter?

We have four classes. First is the tkinterApp class, where we have initialized the three frames and defined a function show_frame which is called every time the user clicks on a button. The StartPage is simple with two buttons to go to Page 1 and Page 2. Page 1 has two buttons, One for Page 2 and another to return to Start Page.


2 Answers

Make each page a frame. Then, all your buttons need to do is hide whatever is visible, then make the desired frame visible.

A simple method to do this is to stack the frames on top of each other (this is one time when place makes sense) and then ,lift() the frame you want to be visible. This technique works best when all pages are the same size; in fact, it requires that you explicitly set the size of containing frame.

The following is a contrived example. This isn't the only way to solve the problem, just proof that it's not a particularly hard problem to solve:

import Tkinter as tk  class Page(tk.Frame):     def __init__(self, *args, **kwargs):         tk.Frame.__init__(self, *args, **kwargs)     def show(self):         self.lift()  class Page1(Page):    def __init__(self, *args, **kwargs):        Page.__init__(self, *args, **kwargs)        label = tk.Label(self, text="This is page 1")        label.pack(side="top", fill="both", expand=True)  class Page2(Page):    def __init__(self, *args, **kwargs):        Page.__init__(self, *args, **kwargs)        label = tk.Label(self, text="This is page 2")        label.pack(side="top", fill="both", expand=True)  class Page3(Page):    def __init__(self, *args, **kwargs):        Page.__init__(self, *args, **kwargs)        label = tk.Label(self, text="This is page 3")        label.pack(side="top", fill="both", expand=True)  class MainView(tk.Frame):     def __init__(self, *args, **kwargs):         tk.Frame.__init__(self, *args, **kwargs)         p1 = Page1(self)         p2 = Page2(self)         p3 = Page3(self)          buttonframe = tk.Frame(self)         container = tk.Frame(self)         buttonframe.pack(side="top", fill="x", expand=False)         container.pack(side="top", fill="both", expand=True)          p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)         p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)         p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)          b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)         b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)         b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)          b1.pack(side="left")         b2.pack(side="left")         b3.pack(side="left")          p1.show()  if __name__ == "__main__":     root = tk.Tk()     main = MainView(root)     main.pack(side="top", fill="both", expand=True)     root.wm_geometry("400x400")     root.mainloop() 
like image 193
Bryan Oakley Avatar answered Sep 28 '22 03:09

Bryan Oakley


Could you do something like this?

import tkinter  def page1():     page2text.pack_forget()     page1text.pack()  def page2():     page1text.pack_forget()     page2text.pack()  window = tkinter.Tk()  page1btn = tkinter.Button(window, text="Page 1", command=page1) page2btn = tkinter.Button(window, text="Page 2", command=page2)  page1text = tkinter.Label(window, text="This is page 1") page2text = tkinter.Label(window, text="This is page 2")  page1btn.pack() page2btn.pack() page1text.pack() 

It seems a lot simpler to me.

like image 45
William Jones Avatar answered Sep 28 '22 04:09

William Jones