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()
1

1 Answer 1

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".

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.