Hello.
In this article, you can find the source for creating multiple windows using Tkinter. Thank you.
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()
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