0

I'm trying to understand more about classes. I want to play around with this example Best way to structure a tkinter application? from Bryan Oakley. But now I have a problem, I want to make a simple destroy function (here in the Toolbar, but get only errors. Or do I need to make all functions in the Mainwindow?

import tkinter as tk
from tkinter import ttk


class MainWindow(tk.Tk):
    def __init__(self, bg="#1f2327", fg="#c4ddf4", *args, **kwargs):
        tk.Tk.__init__(self,  *args, **kwargs)
        self.geometry("1300x850")                                       # Window Options

        self.bg = bg
        self.fg = fg

        self.toolframe = Toolbar(bg=self.bg)                            # import classes and initialize
        self.navbar = Navbar(self)

        self.toolframe.toolframe.pack(ipady=5, fill="x")                # place the widgets from TOOL BAR
        self.navbar.navbar.pack(side="left", ipadx=40, fill="y")


class Toolbar(tk.Frame):                                                # toolbar class
    def __init__(self, bg=None, fg="#c4ddf4", hoverdark="#2b3036",
                 *args, **kwargs):
        tk.Frame.__init__(self,  *args, **kwargs)

        self.bg = bg
        self.fg = fg
        self.hoverdark = hoverdark

        self.toolframe = tk.Frame(bg=self.bg)                           # tool frame

        self.exitstyle = ttk.Style()                                    # set style for the exit button
        self.exitstyle.theme_use("clam")
        self.exitstyle.configure('EXIT.TButton', background=self.bg, foreground=self.fg, borderwidth=0,
                                 focuscolor="none", font=("Bahnschrift", 10), width=5)
        self.exitstyle.map('EXIT.TButton',
                           background=[("pressed", "red"), ("active", "#ff5353")]
                           )

        self.exitbutton = ttk.Button(self.toolframe,                    # exit button
                                     text="\u26CC",
                                     style='EXIT.TButton',
                                     command=self.closeApp  #<------------------------------HERE
                                     )

        self.toolstyle = ttk.Style()                                    # set style for min and max button
        self.toolstyle.theme_use("clam")
        self.toolstyle.configure('TOOL.TButton', background=self.bg, foreground=self.fg, borderwidth=0,
                                 focuscolor="none", font=("Bahnschrift", 10), width=5)
        self.toolstyle.map('TOOL.TButton',
                           background=[("pressed", "red"), ("active", self.hoverdark)]
                           )

        self.maximizebutton = ttk.Button(self.toolframe,                # maximize button
                                         text="\u2610",
                                         style="TOOL.TButton"
                                         )

        self.minimizebutton = ttk.Button(self.toolframe,                # minimize button
                                         text="\u268A",
                                         style="TOOL.TButton")

        self.exitbutton.pack(side="right", fill="y")                        # exit button pack
        self.maximizebutton.pack(side="right", fill="y")                    # maximize button pack
        self.minimizebutton.pack(side="right", fill="y")                    # minimize button pack

    def closeApp(self):             #<-----------------------------------------------HERE
        MainWindow.destroy(self)


class Navbar(tk.Frame):                                                        # navbar class
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.navbar = tk.Frame(bg="black")


if __name__ == "__main__":
    MainWindow()
    tk.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
bangKok
  • 332
  • 2
  • 13
  • 1
    When creating the `Toolbor` object pass a reference of the `MainWindow` object – TheLizzard May 06 '21 at 16:53
  • Please try to reduce the code down to a [mcve]. If your question is about a button that calls `destroy`, we don't need much more than the root window, the button, and a little more code to make it work. – Bryan Oakley May 06 '21 at 18:06
  • The question wasn't only about `destroy` (sry my fault, I try to explain myself bettter next time). The general question was how I can create a function from a class by using this method to use it in an other class. In other cases I can handle it, but this way was new to me. – bangKok May 06 '21 at 18:24

1 Answers1

1

Try using this:

import tkinter as tk
from tkinter import ttk


class MainWindow(tk.Tk):
    def __init__(self, bg="#1f2327", fg="#c4ddf4", *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("1300x850")                                       # Window Options

        self.bg = bg
        self.fg = fg

        self.toolframe = Toolbar(self, bg=self.bg)                      # import classes and initialize
        self.navbar = Navbar(self)

        self.toolframe.toolframe.pack(ipady=5, fill="x")                # place the widgets from TOOL BAR
        self.navbar.navbar.pack(side="left", ipadx=40, fill="y")


class Toolbar(tk.Frame):                                                # toolbar class
    def __init__(self, window, bg=None, fg="#c4ddf4", hoverdark="#2b3036",
                 *args, **kwargs):
        tk.Frame.__init__(self, window, *args, **kwargs)
        self.window = window # You can use the `self.master` that `tk.Frame.__init__` setts up instead

        self.bg = bg
        self.fg = fg
        self.hoverdark = hoverdark

        self.toolframe = tk.Frame(self, bg=self.bg)                           # tool frame

        self.exitstyle = ttk.Style()                                    # set style for the exit button
        self.exitstyle.theme_use("clam")
        self.exitstyle.configure('EXIT.TButton', background=self.bg, foreground=self.fg, borderwidth=0,
                                 focuscolor="none", font=("Bahnschrift", 10), width=5)
        self.exitstyle.map('EXIT.TButton',
                           background=[("pressed", "red"), ("active", "#ff5353")]
                           )

        self.exitbutton = ttk.Button(self.toolframe,                    # exit button
                                     text="\u26CC",
                                     style='EXIT.TButton',
                                     command=self.closeApp  #<------------------------------HERE
                                     )

        self.toolstyle = ttk.Style()                                    # set style for min and max button
        self.toolstyle.theme_use("clam")
        self.toolstyle.configure('TOOL.TButton', background=self.bg, foreground=self.fg, borderwidth=0,
                                 focuscolor="none", font=("Bahnschrift", 10), width=5)
        self.toolstyle.map('TOOL.TButton',
                           background=[("pressed", "red"), ("active", self.hoverdark)]
                           )

        self.maximizebutton = ttk.Button(self.toolframe,                # maximize button
                                         text="\u2610",
                                         style="TOOL.TButton"
                                         )

        self.minimizebutton = ttk.Button(self.toolframe,                # minimize button
                                         text="\u268A",
                                         style="TOOL.TButton")

        self.exitbutton.pack(side="right", fill="y")                        # exit button pack
        self.maximizebutton.pack(side="right", fill="y")                    # maximize button pack
        self.minimizebutton.pack(side="right", fill="y")                    # minimize button pack

    def closeApp(self):             #<-----------------------------------------------HERE
        self.window.destroy()


class Navbar(tk.Frame):                                                        # navbar class
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.navbar = tk.Frame(bg="black")


if __name__ == "__main__":
    window = MainWindow()
    window.mainloop()

I changed it so when MainWindow creates a Toolbar object, it passes self as a parameter. I save that in a .window attribute in the new Toolbar object and call self.window.destroy() whenever the user presses the X

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • oh wow ... you are super fast xD. It helps a lot. Never did this way before and I was shocked ... Now I tried it with other functions, it works. THX! – bangKok May 06 '21 at 17:17
  • @JoeMo No problem. Also I like that `Toolbar` so I might copy those Unicode characters (`\u26cc`, `\u2610`, `\u268a`). Never knew that they existed. – TheLizzard May 06 '21 at 17:19
  • If you want more: https://jrgraphix.net/r/Unicode/2600-26FF . But I only know how to use it in python with "\u....." I think in other program langs you need another string. But not 100% sure. I miss a "home-symbol", I always use \u2630 but a "house-symbol" would be looks better :D. – bangKok May 06 '21 at 17:34
  • @JoeMo Thanks. I might use those to improve [this](https://stackoverflow.com/a/66194808/11106801). – TheLizzard May 06 '21 at 17:37
  • oh I see. Yes maybe it would look better but everyone has a different taste. For myself the Unicode is better when I have problems and I want to share the code. I needn't to stress others with images xD – bangKok May 06 '21 at 17:44