0

I'm trying out this simple piece of code. It was running earlier when I was using pack, but when I changed it to grid, nothing runs after the program is invoked from console. python.exe keeps running as indicated in taskManager, but no GUI gets displayed. Please help.

from Tkinter import *
from tkFileDialog import askopenfilename

def fun():
    fileName = askopenfilename(parent=top, title='Choose a file')
    custName.set(fileName)


root = Tk()
root.geometry('400x150')
root.title('GUI App')
root.resizable(0,0)

top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)    

label = Label(root, text = 'Enter the path upto the file name:')
label.grid(column=0,row=0,sticky='NW')

custName = StringVar(None)
filepath = Entry(root, width ='50', textvariable=custName)
filepath.grid(column=0,row=1,sticky='NW')

browse = Button(root, text="Browse", relief = 'raised', width=8, height=1, command=fun)
browse.grid(column=1,row=1,sticky='E')

quit_button = Button(root, text = 'Exit', relief = 'raised', command = top.quit)
quit_button.grid(column=1,row=3,sticky='SW')

mainloop()
manty
  • 287
  • 5
  • 19
  • 1
    You still have `top.pack(...)`, so it's getting stuck in an infinite loop (see e.g. http://stackoverflow.com/a/3968033/3001761). – jonrsharpe Nov 25 '14 at 17:18

1 Answers1

3

You forgot to change pack to grid on this line:

top.pack(side=TOP)

You cannot mix pack and grid in the same container. Doing so will cause Tkinter to enter an infinite loop as it tries to determine what geometry manager to follow. This in turn will prevent the window from being established and the program will seemingly "freeze".

  • @JoranBeasley: I've used about a dozen GUI toolkits over the years, and by FAR tkinter (and tcl/tk) has the simplest layout system out there. No kidding, if you devote just a single day to understanding it, you'll find it is remarkably simple and powerful. You can create almost any layout imaginable with `grid` and `pack`, and then you have `place` for difficult edge cases and special situations. – Bryan Oakley Nov 25 '14 at 18:05
  • Im gonna have to disagree I have had far less issues with both wx and qt ... but thats just me ... Im not saying you cant its just theres so many little edge cases that break my layout with tk (IE autosizing panels when text changes in them, sizing using text units rather than pixels etc ... ) that said I have made it work every time its just more painful with tkinter imho – Joran Beasley Nov 25 '14 at 18:15