Home > Mobile >  Can't Close tkinter app from class MainFrame(ttk.Frame):
Can't Close tkinter app from class MainFrame(ttk.Frame):

Time:11-13

I am trying to learn tkinter. I started with an example from here; https://www.pythontutorial.net/tkinter/tkinter-object-oriented-frame/. I have all my buttons in class MainFrame(ttk.Frame), I want to use one of the buttons to exit the app but I can't figure out how to do it. Can anyone assist please?

import tkinter as tk
from tkinter import ttk


class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)
        
        app = App()


        self.style2 = {'fg': 'black', 'bg': '#e95420', 'activebackground': 'coral', 'activeforeground': '#2f2f2f'}
        
        self.exit=tk.Button(self , compound=tk.RIGHT, text='exit', width=20, relief="solid")
        self.exit['command'] = self.exit_test
        self.exit.pack(expand=False, padx=0, pady=100, ipadx=10, ipady=10, side=tk.BOTTOM)
        self.exit.configure(self.style2)
        self.pack()

    def exit_test(self):
        app.exit()

class StatusBar(tk.Frame):   
    def __init__(self, container):
        super().__init__(container)
         
        self.variable=tk.StringVar()
        self.variable.set("add the current time here")        
        self.label=tk.Label(self, bd=0, relief="solid", height="2", width="1500", textvariable=self.variable, foreground="white", background='#2f2f2f', font=('helvetica',9))                       
        self.label.pack()       
        self.pack(fill="x", side="bottom", ipady=0, padx=0, pady=0)


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1500x1100")
        

    def exit_app():
        self.container.exit()


if __name__ == "__main__":
    app = App()
    frame = StatusBar(app)
    frame = MainFrame(app)
    app.mainloop()

CodePudding user response:

Here's how to implement what I was suggesting in my comments (calling container.exit from a Button).

import tkinter as tk
from tkinter import ttk


class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        self.style2 = {'fg': 'black', 'bg': '#e95420', 'activebackground': 'coral',
                       'activeforeground': '#2f2f2f'}

        self.exit = tk.Button(self, compound=tk.RIGHT, text='Exit', width=20,
                              relief="solid", command=container.exit)
        self.exit.pack(expand=False, padx=0, pady=100, ipadx=10, ipady=10, side=tk.BOTTOM)
        self.exit.configure(self.style2)
        self.pack()


class StatusBar(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        self.variable = tk.StringVar()
        self.variable.set("add the current time here")
        self.label=tk.Label(self, bd=0, relief="solid", height="2", width="1500",
                            textvariable=self.variable, foreground="white",
                            background='#2f2f2f', font=('helvetica',9))
        self.label.pack()
        self.pack(fill="x", side="bottom", ipady=0, padx=0, pady=0)


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1500x1100")

    def exit(self):
        self.destroy()


if __name__ == "__main__":
    app = App()
    sb = StatusBar(app)
    mf = MainFrame(app)
    app.mainloop()
  • Related