The problem I’m seeing with Tkinter (using Python 2.5.1) is that, as I add elements using the grid() method, they stack on top of each other instead of making a new row. The code will run, but the widgets are all stacked in two rows in the center of the application. Why is this happening?
from Tkinter import *
import random
class Application(Frame):
"""Define Application's constructor"""
def __init__(self, master):
"""Initialize the Frame"""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
#Create the widgets
def create_widgets(self):
self.lblTitle = Label(self, text="Build Your Own Burger")
self.lblTitle.grid(row=0)
self.lblBurgerImg = Label(self, image=PhotoImage(file="burger.gif"))
self.lblTitle.grid(row=1)
self.lblName = Label(self, text="Name:")
self.lblName.grid(row=2, column=0, columnspan=2, sticky=W)
self.entryName = Entry(self)
self.entryName.grid(row=2, column=1, columnspan=2, sticky=W)
self.lblToppings = Label(self, text = "Toppings:")
self.lblTitle.grid(row=3, column=0, sticky=W, columnspan=5, sticky=W)
self.chkCheese = Checkbutton(self, text="cheese")
self.chkCheese.grid(row=3, column=1, sticky=W, columnspan=5, sticky=W)
#main program
#create a root window
root = Tk()
#assign a title for the GUI
root.title("Order Up!")
#Define size of root window
root.geometry("700x700")
#create an instance of your application
app = Application(root)
#start the event loop
root.mainloop()
First off, are you certain this code works for you? I get errors about using the
stickyoption twice on a couple lines. That’s not the problem, however.What you think is happening is not what is actually happening. The widgets aren’t “all” stacked on top of each other: two are invisible because you aren’t calling the
gridmethod on them, and it is only the title that is stacked on top of one other widget because that is where you place it.The problem is that you call
self.lblTitle.grid(...)three times. Two of those three times you were no doubt intending to call thegridmethod on other widgets.