Home > Software design >  How to open a new window which is in another class in tkinter
How to open a new window which is in another class in tkinter

Time:12-17

import tkinter as tk
from tkinter import *
import PIL
from PIL import Image
from PIL import ImageTk, Image
import time


class Sharingan(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("60x60 1465 750")
        self.config()

        self.photo_sharingan = ImageTk.PhotoImage(file="sharingan.png")

#this button has to open a new window on button click:     
        self.btn_sharingan = Button(image=self.photo_sharingan, bd=0,
                                    command=lambda: [self.change_btn_img(),self.open_main_window()])
        self.btn_sharingan.place(x=-3, y=-4)

        self.photo_sharingan2 = ImageTk.PhotoImage(file="sharingan-2.png")
        self.btn_sharingan2 = Button(image=self.photo_sharingan2, bd=0, command=lambda: [self.change_btn_img2()],
                                     highlightbackground="black")
        self.btn_sharingan2.place_forget()

        self.overrideredirect(True)

    def change_btn_img(self):
        self.btn_sharingan.place_forget()
        self.btn_sharingan2.place(x=-3, y=-4)

    def change_btn_img2(self):
        self.btn_sharingan2.place_forget()
        self.btn_sharingan.place(x=-3, y=-4)

#this is the function but how do I make it to initialize and mainloop class-MainWindow
def open_main_window():
    

class MainWindow(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("200x100")



app1 = Sharingan()
app1.mainloop()

So basically I want to open a new window whose code is in another class from a button click. This button is in class1. How can I accomplsh this? The function is 'open_main_window' an the button is 'sharingan'. I just want to open a new window when this button is clicked what code should I write in the function?

CodePudding user response:

To open a new window you use tk.Toplevel() so your function would be:

def open_main_window(self):
   self.newWindow = tk.Toplevel()

For more info on tk.Toplevel() check out this tutorial

  • Related