[Python] Creating multiple windows with Tkinter

Introduction

Hello.

In this article, you can find the source for creating multiple windows using Tkinter. Thank you.

environment

Source code

This is the completed source code. Please try it.

main.py


import tkinter as tk

class Application(tk.Frame):
  def __init__(self,master):
    super().__init__(master)
    self.pack()
    master.geometry("300x300")
    master.title("Base window")

    self.window = []
    self.user = []

    self.button = tk.Button(master,text="Window creation",command=self.buttonClick,width=10)
    self.button.place(x=110, y=150)
    self.button.config(fg="black", bg="skyblue")

  def buttonClick(self):
    self.window.append(tk.Toplevel())
    self.user.append(User(self.window[len(self.window)-1],len(self.window)))

class User(tk.Frame):
  def __init__(self,master,num):
    super().__init__(master)
    self.pack()
    self.num = num
    master.geometry("300x300")
    master.title(str(self.num)+"The second window created")

    self.button = tk.Button(master,text="Confirmation on the console",command=self.buttonClick,width=20)
    self.button.place(x=70, y=150)
    self.button.config(fg="black", bg="pink")

  def buttonClick(self):
    print("This is"+str(self.num)+"This is the second window created.")

def main():
  win = tk.Tk()
  app = Application(win)
  app.mainloop()

if __name__ == '__main__':
  main()

Actual operation

When I run this program, the base window is displayed first. Then click the button in the base window to create and display a new window. If you click the button on the created window, the console will show you how many windows this window was created on.

You can create as many new windows as you like by clicking the button in the base window, so give it a try.

Thank you for reading this far.

Recommended Posts

[Python] Creating multiple windows with Tkinter
[Python] Show multiple windows in Tkinter
Develop Windows apps with Python 3 + Tkinter (Preparation)
Python starting with Windows 7
Develop Windows apps with Python 3 + Tkinter (exe file)
Programming with Python and Tkinter
Python with VS Code (Windows 10)
Run python with PyCharm (Windows)
Creating an egg with python
Run Label with tkinter [Python]
Time synchronization (Windows) with Python
Face detection with YOLO Face (Windows10, Python3.6)
Build mlpy with python3.3 (64bit) (windows 64bit)
Animate multiple still images with Python
Creating a GUI as easily as possible with python [tkinter edition]
Use Windows 10 speech synthesis with Python
Multiple integrals with Python and Sympy
Getting started with Python 3.8 on Windows
[Python Windows] pip install with Python version
Play video with sound with python !! (tkinter / imageio)
Creating a simple PowerPoint file with Python
Creating an image splitting app with Tkinter
Note when creating an environment with python
Building a Python 3.6 environment with Windows + PowerShell
Creating a python virtual environment on Windows
Process multiple lists with for in Python
Question: Multiple integrals with python don't work
Manage multiple Python versions with update-alternatives (Ubuntu)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Python on Windows
Scraping with Python
Python with Go
python windows environment
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
[Python] Tkinter template
Cast with python
Project cannot be created with Python3.5 (Windows) + django1.7.1
GUI image cropping tool made with Python + Tkinter
[Python] How to draw multiple graphs with Matplotlib
Python (Windows 10) Virtual Environment / Package with VS Code
Procedure for creating a LineBot made with Python
GUI automation with Python x Windows App Driver
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Manage Python multiple version environment with Pythonz, virtualenv
Convert Windows epoch values to date with python
Use multiple versions of python environment with pyenv
Image Processing with Python Environment Setup for Windows
Developed and verified with multiple python versions with direnv
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv