0

Here, I need to place the button in homeFrame. But I can't place the button there. What's the issue? Please resolve it. Here is the code.

from tkinter import *


root = Tk()
root.state('zoomed')
root.configure(bg='#498240')

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

headFrame = Frame(root,width=screen_width,height=120,bd=10,bg='#498240',relief=RIDGE).grid(row=0,column=0)
headlable = Label(headFrame,text='Super Market',fg='white',bg='#498240',font=('times new roman',55,'bold')).grid(row=0,column=0)

homeFrame = Frame(root,width=screen_width,height=screen_height-200,bd=10,bg='#498240',relief=RIDGE).grid(row=1,column=0)
homeBtn = Button(homeFrame,text='Customer Management').grid(row=0,column=0)


root.mainloop()

Screenshot: enter image description here

Here I have mentioned the location of the button please help me to place the button there.

Xor96
  • 424
  • 1
  • 3
  • 13

1 Answers1

2

You've fallen in to the standard trap of creating a widget and using grid on the same row.

Split them up like this

homeFrame = Frame(root,width=screen_width,height=screen_height-200,bd=10,bg='#498240',relief=RIDGE)
homeFrame.grid(row=1,column=0)
homeBtn = Button(homeFrame,text='Customer Management')
homeBtn.grid(row=0,column=0)

If you do

homeFrame = Frame(root).grid()

homeFrame will contain "None" and any widget that you try to place in "None" will have root as its parent. But if you do

homeFrame = Frame(root)

homeFrame will contain a valid instance of a tkinter frame in which widgets can be placed.

Lesson is that you should never create a widget on the same row as you use grid/pack/place (unless you don't ever refer to that widget again)

PS, a tkinter frame will automatically re-size to fit any child widgets, you can force it to not do this by following the guidance at this question How to stop Tkinter Frame from shrinking to fit its contents?

scotty3785
  • 6,763
  • 1
  • 25
  • 35