efultz
efultz

Reputation: 1265

python tkinter button in frame

It feels this should be easy but not as much as I would hope. All I want to do is put a button in a frame. My code is coloring the frame so I can validate the button is where I want to put it and as you can see, below, my code is not doing what I want/think. I expect my code to put the radio button inside the yellow frame - not under it.

from tkinter import *

class apiMain:

    def main(self):
        master=Tk()

        topframe = Frame(master, bg="Lemon chiffon", width=500, height=50).pack(side = TOP)        

        v = IntVar()
        crbutton = Radiobutton(topframe, text = "change request", variable = v, value = 'cr')
        crbutton.pack(side = LEFT, padx = 10)        

        mainloop()

enter image description here

Upvotes: 0

Views: 1113

Answers (1)

MrAlexBailey
MrAlexBailey

Reputation: 5289

When you assign topframe like this:

topframe = Frame(master, bg="Lemon chiffon", width=500, height=50).pack(side = TOP)

You are essentially writing topframe = None, because pack() always returns None. Because of this, you're assigning the master of your radio button to None which defaults to the main window. Split your code up, so that topframe references the actual Frame object:

topframe = Frame(master, bg="Lemon chiffon", width=500, height=50)
topframe.pack(side = TOP)

Upvotes: 3

Related Questions