Python Forum

Full Version: Creating a frame with 4 command buttons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello python users:

The following code is to create a frame with four command buttons. It does this. However, before it creates the frame with four command buttons, it creates a frame with no buttons. Why does it create the frame with no buttons?


import tkinter as tk
myfont = "Helvitica 30"
mybg = "lightskyblue"


class Main:

    def __init__(self, root):
        self.root = tk.Tk()
        self.root.geometry("500x500")
        self.createfstframe()

    def createfstframe(self):
        fstframe = tk.Frame(root)
        fstframe.pack(padx=100, pady=100)
        self.Hellobn=tk.Button(fstframe,text = "Hello", bg = mybg, font = myfont)
        self.Hellobn.pack(fill = tk.BOTH, expand = 1)
        self.Howyoubn = tk.Button(fstframe, text="How are you? ",bg = mybg ,font = myfont)
        self.Howyoubn.pack(fill = tk.BOTH, expand = 1)
        self.clickherebn = tk.Button(fstframe, text = "Click here to start", bg = "deepskyblue",font = myfont)
        self.clickherebn.pack(fill = tk.BOTH, expand = 1)
        self.quitbn = tk.Button(fstframe,text = "Quit", font = myfont, bg = "red", command = fstframe.quit)
        self.quitbn.pack(fill = tk.BOTH, expand = 1)


root = tk.Tk()
b=Main(root)
root.mainloop()
Heyjoe,

I am not TK user, but from your description, I suspect you did not add the widget to the frame. So you can only see the frame.
    def createfstframe(self):
        fstframe = tk.Frame(root)  # should be self.root
There is no root, so fstframe has no parent. Because it has no parent the window manager gives it its' own window.
You have created two instance of tk.Tk()
class Main:
 
    def __init__(self, root):
        self.root = tk.Tk()
        ...
        ...

    def createfstframe(self):
        fstframe = tk.Frame(root) # this is using the global defined root
        ...
        ...

root = tk.Tk()
b=Main(root)
root.mainloop()
you only need one, change it to
class Main:
 
    def __init__(self, root):
        self.root = root # store the passed in root
        ...
        ...

    def createfstframe(self):
        fstframe = tk.Frame(self.root) # use the passed in root
        ...
        ...

root = tk.Tk()
b=Main(root)
root.mainloop()
Thanks all.

I have changed the code and it works.

I created this code after watching tutorials on classes and Tkinter.

There is still one thing that makes absolutely no sense to me. The second to the last line of code.

b=Main(root)
Why is Main(root) assigned to the variable? What does this line of code mean? Why does my code not work without this line of code?
How about you figure that out by yourself. First test if just calling Main(root)without the variable assignment makes your code work. If so, maybe the assignment to a variable is not important. What is the type of "b"? Is it used anywhere?

Figuring this out by yourself will result in a much deeper understanding than us answering your question. Deeper even than what you are learning by watching tutorials. I am a firm believer that you do not learn how to code by writing code. You learn how to code by debugging code. It is during debugging that you really learn what everything does and how it all works together.