0

I am currently learning Python and Tkinter. Therefore I've written a small script:

#importing Stuff
import tkinter as tk
#Userinterface
class Gui(tk):
    #creating a frame for the grid
    class creating_spielfeld(tk):
        def __init__(self, window):
            self.spielfeld = tk.Frame(window)
            self.spielfeld.pack(fill="both", expand=True)

    #creating the Button's
    class Buttons(tk):
        def __init__(self, spielfeld):
            self.button1 = tk.Button(spielfeld, text="1")
            self.button2 = tk.Button(spielfeld, text="2")
            self.button3 = tk.Button(spielfeld, text="3")
            self.button4 = tk.Button(spielfeld, text="4")
            self.button5 = tk.Button(spielfeld, text="5")
            self.button6 = tk.Button(spielfeld, text="6")
            self.button7 = tk.Button(spielfeld, text="7")
            self.button8 = tk.Button(spielfeld, text="8")
            self.button9 = tk.Button(spielfeld, text="9")
        #"placing" Buttons
            self.button1.grid(row = 0, column = 0, sticky="nsew")
            self.button2.grid(row = 0, column = 1, sticky="nsew")
            self.button3.grid(row = 0, column = 2, sticky="nsew")
            self.button4.grid(row = 1, column = 0, sticky="nsew")
            self.button5.grid(row = 1, column = 1, sticky="nsew")
            self.button6.grid(row = 1, column = 2, sticky="nsew")
            self.button7.grid(row = 2, column = 0, sticky="nsew")
            self.button8.grid(row = 2, column = 1, sticky="nsew")
            self.button9.grid(row = 2, column = 2, sticky="nsew")
        #configuring the grid to be resizable
            self.tk.Grid.rowconfigure(spielfeld, index=0, weight=1)
            self.tk.Grid.rowconfigure(spielfeld, index=1, weight=1)
            self.tk.Grid.rowconfigure(spielfeld, index=2, weight=1)
            self.tk.Grid.columnconfigure(spielfeld, index=0, weight=1)
            self.tk.Grid.columnconfigure(spielfeld, index=1, weight=1)
            self.tk.Grid.columnconfigure(spielfeld, index=2, weight=1)

        #Credits Label + Frame
        class credits(tk):
            def __init__(self):
                self.credits_frame = tk.Frame(window)
                self.credits_frame.pack(side="bottom")
                self.credit_label = tk.Label(credits_frame, text="Made with ♥ by Boas", font=("Rockwell"))
                self.credit_label.pack(side="bottom")

#creating window
def run_window():
    window = tk.Tk()
    app = Gui(window)
    window.mainloop()
if __name__ == '__main__':
    run_window()

I read this Post but obviously it's not working :/

This is almost the same, and it's working:

import tkinter as tk
    window = tk.Tk()
    frame1 = tk.Frame(window)
    frame1.pack(fill="both", expand=True)
    
    button1 = tk.Button(frame1, text="1")
    button2 = tk.Button(frame1, text="2")
    button3 = tk.Button(frame1, text="3")
    button4 = tk.Button(frame1, text="4")
    button5 = tk.Button(frame1, text="5")
    button6 = tk.Button(frame1, text="6")
    button7 = tk.Button(frame1, text="7")
    button8 = tk.Button(frame1, text="8")
    button9 = tk.Button(frame1, text="9")
    
    button1.grid(row = 0, column = 0, sticky="nsew")
    button2.grid(row = 0, column = 1, sticky="nsew")
    button3.grid(row = 0, column = 2, sticky="nsew")
    button4.grid(row = 1, column = 0, sticky="nsew")
    button5.grid(row = 1, column = 1, sticky="nsew")
    button6.grid(row = 1, column = 2, sticky="nsew")
    button7.grid(row = 2, column = 0, sticky="nsew")
    button8.grid(row = 2, column = 1, sticky="nsew")
    button9.grid(row = 2, column = 2, sticky="nsew")
    
    tk.Grid.rowconfigure(frame1, index=0, weight=1)
    tk.Grid.rowconfigure(frame1, index=1, weight=1)
    tk.Grid.rowconfigure(frame1, index=2, weight=1)
    tk.Grid.columnconfigure(frame1, index=0, weight=1)
    tk.Grid.columnconfigure(frame1, index=1, weight=1)
    tk.Grid.columnconfigure(frame1, index=2, weight=1)
    
    frame2 = tk.Frame(window, background="red")
    frame2.pack(side="bottom")
    credits = tk.Label(frame2, text="Made with ♥ by Boas", font=("Rockwell"))
    credits.pack(side="bottom")
    window.mainloop()

I don't know whats the Problem. Later I'll assign functions to the Buttons to change variables in a list. For a little game. But that doesn't matter rn. Any ideas why the second one is working but the first on isn't?

Thanks for all Answers

  • Why do you think the code isn't working? What is it doing, and how is that different from what you expect? – Bryan Oakley Mar 29 '22 at 18:57
  • 1
    You aren't actually following the structure of the code posted in the answer you linked to. Why are your classes attempting to inherit from the module `tk`? That makes no sense. What you wanted to do was pass in a reference of your `window` object into `Gui.__init__`, so that your widgets have a reference to the `tk.Tk` parent. – Paul M. Mar 29 '22 at 21:31

0 Answers0