0
from tkinter import *


win = Tk()
win.title('McDonald ordering system')
win.geometry('600x300')


lbtitle = Label(win,text='Welcome to McDonald',bg='yellow')
lbtitle.grid(row=0,column=1,columnspan=3)

f1 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f1.grid(row=1,column=0,rowspan=3)

f2 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f2.grid(row=1,column=1,rowspan=3)

f3 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f3.grid(row=1,column=2,rowspan=3)

lb1 = Label(f1,text='Big Mac')
lb1.grid(row=0,column=0,padx=10,pady=10)

am1 = IntVar()
en1 = Entry(f1,textvariable=am1,width=5)
en1.grid(row=0,column=1,padx=10,pady=10)

lb2 = Label(f1,text='Cheeseburger')
lb2.grid(row=1,column=0,padx=10,pady=10)

am2 = IntVar()
en2 = Entry(f1,textvariable=am2,width=5)
en2.grid(row=1,column=1,padx=10,pady=10)

lb3 = Label(f1,text='Nuggets')
lb3.grid(row=2,column=0,padx=10,pady=10)

am3 = IntVar()
en3 = Entry(f1,textvariable=am3,width=5)
en3.grid(row=2,column=1,padx=10,pady=10)

lb4 = Label(f1,text='Chicken')
lb4.grid(row=3,column=0,padx=10,pady=10)

am4 = IntVar()
en4 = Entry(f1,textvariable=am4,width=5)
en4.grid(row=3,column=1,padx=10,pady=10)


lb5 = Label(f2,text='Cheesecake')
lb5.grid(row=0,column=0,padx=10,pady=10)

am5 = IntVar()
en5 = Entry(f2,textvariable=am5,width=5)
en5.grid(row=0,column=1,padx=10,pady=10)

lb6 = Label(f2,text='Chocolate Cake')
lb6.grid(row=1,column=0,padx=10,pady=10)

am6 = IntVar()
en6 = Entry(f2,textvariable=am6,width=5)
en6.grid(row=1,column=1,padx=10,pady=10)

lb7 = Label(f2,text='McFlurry')
lb7.grid(row=2,column=0,padx=10,pady=10)

am7 = IntVar()
en7 = Entry(f2,textvariable=am7,width=5)
en7.grid(row=2,column=1,padx=10,pady=10)

lb8 = Label(f2,text='Pepsi')
lb8.grid(row=3,column=0,padx=10,pady=10)

am8 = IntVar()
em8 = Entry(f2,textvariable=am8,width=5)
em8.grid(row=3,column=1,padx=10,pady=10)


win.mainloop()

The frame works now but however if I change the height and width of the frames and I run each time the size is still the same. I do not know what's wrong but I already searched through the books mentioned that size can be set at the beginning. So how could I make the height and width adjustable? Is the size of frames count with the elements inside or I can set the sizes at the beginning?

  • the frame automatically size to the widget it contains. In your case there is nothing in there, so the frame get a width of 0 and a height of 0. The frame is the, you just cant see it. I wrote a few minutes an answer for it. check https://stackoverflow.com/questions/63189654/trouble-using-tkinter-grid – Thingamabobs Jul 31 '20 at 12:12
  • but when I set the width and height the frame still does not appear although I added some elements inside the elements are not inside the frame – Dominic Sham Jul 31 '20 at 12:19
  • I cant see any widget that are assigned to your frames. please make a full exampel of whats wrong – Thingamabobs Jul 31 '20 at 12:20
  • @DominicSham you have to disable shrinking of the frames to the size of their childen with `grid_propagate`. I updated my answer accordingly. – Stefan Scheller Jul 31 '20 at 13:06
  • Nowhere in the code you presented do you try to set or change the height of anything. – Bryan Oakley Jul 31 '20 at 15:04

1 Answers1

0

As you wrote in your question you imported Frame through from tkinter import *. So you don't need to import it again.

To make the window visible add win.mainloop() at the end of your script.

Update:

This is how your program looks like on my system when executing it from the terminal (and having win.mainloop() at the end of the script).

enter image description here

Update 2:

I've changed this section by removing columnspan from the label and disabling resizing of the frame with grid_propagate:

lbtitle = Label(win,text='Welcome to McDonald',bg='yellow')
lbtitle.grid(row=0,column=1)

f1 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f1.grid(row=1,column=0,rowspan=3)
f1.grid_propagate(0)

f2 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f2.grid(row=1,column=1,rowspan=3)
f2.grid_propagate(0)

f3 = Frame(win,bd=2,width=200,height=250,relief=GROOVE)
f3.grid(row=1,column=2,rowspan=3)
f3.grid_propagate(0)

The results looks like this:

enter image description here

Stefan Scheller
  • 903
  • 1
  • 11
  • 20
  • This do not answer the question. Also it isnt requierd to explicit call the mainloop. Its just the pythonic way to do it. Cause explicit is better than implicit. – Thingamabobs Jul 31 '20 at 12:32
  • 1
    @Atlas435 It may be not necessary when you run the script in IDLE or some IDE that are made by using tkinter as well, but it is definitely necessary when you run the script in a terminal or console. – acw1668 Jul 31 '20 at 12:35
  • No difference after I added win.mainloop() – Dominic Sham Jul 31 '20 at 12:38
  • @DominicSham Where did you add the line? It has to be after you added all the widgets because the function is not left until the window is claused. – Stefan Scheller Jul 31 '20 at 12:38
  • @acw1668 thank for letting me know, I wasnt aware of it. But in this case it does not seem helpfull for me. – Thingamabobs Jul 31 '20 at 12:39
  • @Atlas435: I tested it with the code in the question and after adding the line the window/frame appeared. What would you consider helpful then? – Stefan Scheller Jul 31 '20 at 12:41
  • @Stefan Scheller which line do you mean? – Dominic Sham Jul 31 '20 at 12:41
  • @DominicSham This one: `win.mainloop()` – Stefan Scheller Jul 31 '20 at 12:42
  • @DominicSham Yes. For me this solved the problem. Can you edit/update your question with the new code version? – Stefan Scheller Jul 31 '20 at 12:43
  • @StefanScheller if I run this code without win.mainloop() and give a frame the bg='red' a small red line is on my window. If I explicit call mainloop the same thing happens. It just dosent fixed the problem. So it isnt an answer. If you solve it I will remove my downvote and give it a upvote. – Thingamabobs Jul 31 '20 at 12:44
  • @Atlas435 Thanks for explaining. On what system are you running the code? I am running this in the terminal on Linux and as stated above, adding the line solved it for me. – Stefan Scheller Jul 31 '20 at 12:46
  • I need to apologize then. A hint: f3 is empty.. Maybe we need to ask him what he thinks a frame looks like. – Thingamabobs Jul 31 '20 at 12:49
  • @DominicSham: You should not post the updates as answers but rather edit your original question. – Stefan Scheller Jul 31 '20 at 12:50
  • 1
    @Atlas435 You are right, there was quite some misunderstanding. I thought he was asking why the window does not appear but it seems he was asking why the frames don't show a relief/groove. – Stefan Scheller Jul 31 '20 at 13:09
  • Now it works after I add f1.grid_propagate(0) and thanks very much – Dominic Sham Jul 31 '20 at 13:11
  • @DominicSham Please consider [accepting the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it solved your problem. – Stefan Scheller Jul 31 '20 at 13:20