Python Forum
Creating a frame with 4 command buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a frame with 4 command buttons
#1
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()
Reply
#2
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.
Reply
#3
    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.
Reply
#4
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()
Reply
#5
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?
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Programmatically creating buttons that remember their positions Clunk_Head 6 1,273 Jun-22-2023, 02:51 PM
Last Post: deanhystad
  [Tkinter] Command button, then more command buttons Heyjoe 4 2,820 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] Creation of Buttons with Shared Command Inside Class MulliganAgain 1 1,685 Jul-08-2020, 06:22 PM
Last Post: Yoriz
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 8,944 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] How to add an argument to a buttons command call petterg 2 3,094 Jun-20-2019, 09:05 PM
Last Post: petterg
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 11,001 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 6,370 Jan-29-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020