Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put a Button into a frame in python tkinter

Tags:

python

tkinter

i am trying template my tkinter application. But i cant put buttons into my Frames.

Is Frame the right control for use as a template? or is there something different.

from Tkinter import *

root = Tk()

frame = Frame(root, height="200", width="200", bg="green").pack()
b = Button(frame, text="Hell World").pack(padx="10", pady="10")

z = Label(root, text="OUTSIDE").pack()

root.mainloop()

this is my code.

the code runs, but the button is beneath the green frame. But i want to have this button inside of the frame.

Is this possible?

like image 336
Ipad Avatar asked Jun 07 '26 07:06

Ipad


1 Answers

frame = Frame(root, height="200", width="200", bg="green").pack()

After this line executes, frame is equal to None because that's what pack() returns. You need to assign a widget and pack it on separate lines if you want to keep a reference to it.

frame = Frame(root, height="200", width="200", bg="green")
frame.pack()

Same for b and z, but you never use them so they don't matter as much.

like image 153
Kevin Avatar answered Jun 10 '26 17:06

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!