Home > Software engineering >  There are two tkinter windows are coming, should be only one
There are two tkinter windows are coming, should be only one

Time:07-31

when I am running a tkinter app, where I can see two windows are coming not sure why There should only be one window of the specified size in the geometry method.

Tkinter app

class Main:

    style = ttk.Style()
    style.map("C.TButton",
    foreground=[('pressed', 'red'), ('active', 'blue')],
    background=[('pressed', '!disabled', 'black'), ('active', 'white')]
    )

    def selectReports(self):
        messagebox.showinfo("EDP", "All reports")

    def showReports(self):
        messagebox.showinfo("EDP", "Select reports")

    #Report Display Window
    # def reviewReport():
    #     win = Toplevel(root)
    #     win.geometry("626x431")

    def Show_Page(self):
        program = tk.Tk()
        program.geometry("626x431")
        monitor = ttk.Button(name="",text="Monitor",command=self.showReports,style="C.TButton")
        monitor.pack(pady=100)
        review = ttk.Button(name="",text="Review",command=self.selectReports,style="C.TButton")
        review.pack(pady=0)
        program.mainloop()
    
# Main method 
if __name__ == "__main__":
    objectMain = Main()
    objectMain.Show_Page()

CodePudding user response:

When the following lines are executed, a root window is implicitly created:

class Main:
    # below line will create a root window implicitly
    style = ttk.Style()
    style.map("C.TButton",
    foreground=[('pressed', 'red'), ('active', 'blue')],
    background=[('pressed', '!disabled', 'black'), ('active', 'white')]
    )
    ...

You need to move them into Show_Page():

class Main:
   ...
    def Show_Page(self):
        program = tk.Tk()
        # code moved here
        style = ttk.Style()
        style.map("C.TButton",
        foreground=[('pressed', 'red'), ('active', 'blue')],
        background=[('pressed', '!disabled', 'black'), ('active', 'white')]
        )
        program.geometry("626x431")
        monitor = ttk.Button(name="",text="Monitor",command=self.showReports,style="C.TButton")
        monitor.pack(pady=100)
        review = ttk.Button(name="",text="Review",command=self.selectReports,style="C.TButton")
        review.pack(pady=0)
        program.mainloop()
  • Related