user4512750
user4512750

Reputation:

tkinter frames not working correctly

I am messing around with the tkinter module for the first time and not quite sure why my widgets aren't being displayed in the correct frame?

"text" and "entry" widgets should be displayed at the top of the top half of the window and "button" and "output" widgets at the top of the bottom half of the window?

Thanks

from tkinter import *

main_window = Tk()
main_window.title("X SQUARED CALCULATOR")
main_window.geometry("300x300")
mw_frame1 = Frame(main_window).pack()
mw_frame2 = Frame(main_window).pack()

text_widget1 = Label(mw_frame1, text="Please enter a value:").pack(side=TOP)
entry_widget1 = Entry(mw_frame1, text="Please enter a value.").pack(side=TOP)
button_widget1 = Button(mw_frame2, text='Press to calculate!').pack(side=TOP)
output_widget1 = Label(mw_frame2, text="THIS IS WHERE THE NUMBER WILL APPEAR").pack(side=TOP)

main_window.mainloop()

Upvotes: 1

Views: 3421

Answers (2)

Sagar
Sagar

Reputation: 1

text_widget1 = Label(mw_frame1, text="Please enter a value:").pack(side=TOP)

Instead write pack separately like this :

text_widget1 = Label(mw_frame1, text="Please enter a value:")
text_widget1.pack(side=TOP)

It will work.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385880

The problem is this:

mw_frame1 = Frame(main_window).pack()

This sets mw_frame1 to None, because pack() returns None. Therefore, when you try to make other widgets children of this widget, they actually become children of the root window. Because they are children of the root window, they are being packed in a place you don't expect.

Move your calls to pack() to separate statements:

mw_frame1 = Frame(...)
mw_frame2 = Frame(...)
...
mw_frame1.pack(...)
mw_frame2.pack(...)

Upvotes: 5

Related Questions