[Python] Show multiple windows in Tkinter

Introduction

Explains how to display multiple Window screens in Tkinter.

result

Tkinter classification template Was used. Follow the steps below to assemble the cord.

  1. Prepare the Win1 class for Window 1.
  2. Prepare the Win2 class for Window 2.
  3. Define the behavior to open Windows 2 in the Call back function (action when pressing Button) of Win1 class.

bandicam-2020-05-11-11-46-00-975.gif

Programming code

import tkinter as tk
from tkinter import ttk

class Win1(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        self.master.geometry("400x400")
        self.master.title("window 1")
        self.create_widgets()

    def create_widgets(self):
        # Button
        self.button_new_win = ttk.Button(self)
        self.button_new_win.configure(text="Open Window 2")
        self.button_new_win.configure(command = self.new_window)
        self.button_new_win.pack()

    #Call back function
    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Win2(self.newWindow)



class Win2(tk.Frame):
    def __init__(self,master):

        super().__init__(master)
        self.pack()
        self.master.geometry("300x300")
        self.master.title("window 2")
        self.create_widgets()

    def create_widgets(self):
        # Button
        self.button_quit = ttk.Button(self)
        self.button_quit.configure(text="Close Window 2")
        self.button_quit.configure(command=self.quit_window)
        self.button_quit.pack()

    def quit_window(self):
        self.master.destroy()


def main():
    root = tk.Tk()
    app = Win1(master=root)#Inherit
    app.mainloop()

if __name__ == "__main__":
    main()

Reference material

  1. [Python] Tkinter template

2. Tkinter example code for multiple windows

Recommended Posts

[Python] Show multiple windows in Tkinter
[Python] Creating multiple windows with Tkinter
Show pyramids in Python
Show decimal point in Python
Multiple regression expressions in Python
Avoid multiple loops in Python
Python install in 2 lines @Windows
Prohibit multiple launches in python
Extract multiple list duplicates in Python
Hit a command in Python (Windows)
Put MeCab in "Windows 10; Python3.5 (64bit)"
Windows10: Install MeCab library in python
Statistical test (multiple test) in Python: scikit_posthocs
Delete multiple elements in python list
GUI creation in python using tkinter 2
Handle multiple python versions in one jupyter
GUI creation in python using tkinter part 1
Using venv in Windows + Docker environment [Python]
Send email to multiple recipients in Python (Python 3)
[Python] [Windows] Serial communication in Python using DLL
[Python] [Windows] Take a screen capture in Python
Develop Windows apps with Python 3 + Tkinter (Preparation)
Process multiple lists with for in Python
Tkinter could not be imported in Python
Python garbled in Windows + Git Bash environment
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Python on Windows
Unittest in python
python windows environment
Epoch in Python
Discord in Python
Python installation (Windows)
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python