Home > Software engineering >  Writing a class under another class using function
Writing a class under another class using function

Time:01-31

I am writing Python code using classes and tkinter, and I want to open another window when I click a button in the main window. I want to design this new window different from the main and add some properties (function) to it which are not in the main window.

Let me show you my code:

from tkinter import *

class Uygulama(object):
    def __init__(self):
        self.araclar()

    def araclar(self):
        self.etiket1 = Label(text="WELCOME TO MY PROGRAM", fg="blue", font="Times 15 bold", 
           bg="grey")
        self.etiket1.pack()

        self.etiket2 = Label(text="Hello world", fg="black", font="Times 11 bold", bg="grey")
        self.etiket2.place(rely=0.95, relx=0.70)
     
        self.dugme5=Button(text="click here to open new window", command=self.bk4, 
               fg="black", 
            bg="blue", font="bold")
        self.dugme5.place(relx=0.38, rely=0.78)

    def bk4(self):
        class Uygulama2:
            def __init__(self):
                self.araclar2()
            def araclar2(self):
                self.dugme6=Button(text="click for pinting", 
                command=self.bk5, fg="black", 
                       bg="red",font="bold")
                self.dugme6.place(relx=0.5,rely=0.5)

            def bk5(self):
                print("hello world")
        

        pencere2 = Tk()
        pencere2.title("This is a new windows")
        pencere2.geometry("390x350 860 30")
        pencere2.resizable(width=False, height=False)
        pencere2.configure(bg="grey")
        uyg2 = Uygulama2()
        pencere2.mainloop()
    

pencere = Tk()
pencere.title("This is main window")
pencere.geometry("550x550 300 30")
pencere.resizable(width=False, height=False)
pencere.configure(bg="grey")
uyg = Uygulama()
mainloop()

My problem: When I click to the button in main windows, another window opened, but the button which must be on the second window appears on the main window. How can I move it to the second window?

Moreover, I am planning to add more functions and widgets to the second window, but I do not want them to appear or run in the main window which is Pencere=Tk().

What's more, is it possible to add a third window which is written using classes and opened on the second window like this?

CodePudding user response:

This is a running version of your code:

from tkinter import *


class Uygulama(Tk):
    def __init__(self):

        super().__init__()

        # Title of the main window
        self.title("This is main window")
        # Set the size and position of the window
        self.geometry("550x550 300 30")
        # Disable resizing of the window
        self.resizable(width=False, height=False)
        # Set the background color of the window
        self.configure(bg="grey")

        # Create a label with text "WELCOME TO MY PROGRAM"
        self.etiket1 = Label(self, text="WELCOME TO MY PROGRAM", fg="blue", font="Times 15 bold",
                             bg="grey")
        # Pack the label
        self.etiket1.pack()

        # Create a label with text "Hello world"
        self.etiket2 = Label(self, text="Hello world", fg="black",
                             font="Times 11 bold", bg="grey")
        # Place the label on the window
        self.etiket2.place(rely=0.95, relx=0.70)

        # Create a button that opens a new window when clicked
        self.dugme5 = Button(self, text="click here to open new window", command=self.bk4,
                             fg="black",
                             bg="blue", font="bold")
        self.dugme5.place(relx=0.38, rely=0.78)

    # Function to create the new window
    def bk4(self):
        Uygulama2(self)


class Uygulama2(Toplevel):
    def __init__(self, master):
        super().__init__(master)

        # Title of the new window
        self.title("This is a new window")
        # Set the size and position of the window
        self.geometry("390x350 860 30")
        # Disable resizing of the window
        self.resizable(width=False, height=False)
        # Set the background color of the window
        self.configure(bg="grey")

        # Create a button that prints "hello world" when clicked
        self.dugme6 = Button(self, text="click for printing",
                             command=self.bk5, fg="black",
                             bg="red", font="bold")
        self.dugme6.place(relx=0.5, rely=0.5)

        # Create a button that opens a new window when clicked
        self.dugme7 = Button(self, text="click here to open new window", command=self.bk6,
                             fg="black",
                             bg="blue", font="bold")
        self.dugme7.pack()

    # Function to print "hello world"
    def bk5(self):
        print("hello world")

    # Function to create the new window
    def bk6(self):
        Uygulama3(self)


class Uygulama3(Toplevel):
    def __init__(self, master):
        super().__init__(master)

        # Title of the new window
        self.title("This is another new window")
        # Set the size and position of the window
        self.geometry("390x350 1260 30")
        # Disable resizing of the window
        self.resizable(width=False, height=False)
        # Set the background color of the window
        self.configure(bg="grey")

        # Create a label with text "Test window 3"
        self.etiket3 = Label(self, text="Test window 3", fg="black",
                             font="Times 11 bold", bg="grey")
        # pack the label on the window
        self.etiket3.pack()


uyg = Uygulama()
uyg.mainloop()

I have added another button which opens another window when pressed to show you how you could implement that.

  • Related