Here's my example of how I did it; not necessarily the best way but fairly functional:
import Tkinter,tkMessageBox,ttk
class mygui(Tkinter.Tk):
def __init__(self, *args, **kwargs):if __name__=='__main__':
Tkinter.Tk.__init__(self, *args, **kwargs)# Define all the widget callback functions for the class
self.title('My GUI Main Window')
# Making a fancy frame here with a blue border just for fun
# Bottom layer is a frame with blue fill
frm_0 = Tkinter.Frame(self, bg="blue")
# This is the main frame that will be on top of the blue frame
f = Tkinter.Frame(frm_0)
# Putting some text in the frame just to have a frame.
# Actual GUI would have more stuff
self.mylabel = Tkinter.Label(f,text="This is a label ",justify="left")
self.mylabel.pack(side="left",padx=10,pady=10)
# Place the main frame inside the blue frame with some padding
# so that the blue of the lower layer shows all around it
f.pack(fill="both",padx=5,pady=5)
frm_0.pack()
# Place the bottom frame where I want it (in the center)
frm_0.place(relx=0.5,rely=0.5,anchor="center")
# Make this example gui the size of the packed controls,
# in the middle of the screen
self.update_idletasks()
width = frm_0.winfo_width()
height = frm_0.winfo_height()
x = self.winfo_screenwidth() // 2 - width // 2
y = self.winfo_screenheight() // 2 - height // 2
self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
# Initialize the state machine to the first state
self.state = 0
# Initialize other variables in this class
self.mytext = ''
self.text_accepted = False
self.abort_selected = False
def accept(self, event=None):
self.mytext = self.e.get()def close_top(self):
self.text_accepted = True
self.top.destroy()
self.mytext = ''# Main executive loop. This is where the part of the program that
self.abort_selected = True
self.top.destroy()
# actually does things would go
def executive(self):
if (self.state == 0):
# First state, for doing stuff before popping up the dialogelif (self.state == 1):
print "Doing stuff in first state."
# Set next state, schedule the next call of exeutive() and exit
# In this example there is only one state before dialog; could
# be any number of states before the dialog
self.state = 1
self.executive()
# Draw dialog popupelif (self.state == 2):
self.top = Tkinter.Toplevel()
msg = 'Enter text and press accept'
self.top.title(msg)
frm1 = Tkinter.Frame(self.top)
self.e = ttk.Entry(frm1)
self.e.config(width=(len(msg)+20))
self.e.insert(0,"default text")
self.e.pack(side="left",padx=5,pady=5)
b = Tkinter.Button(frm1, text="Accept", command=self.accept)
b.pack(side='left',padx=5,pady=5)
frm1.pack()
self.top.protocol("WM_DELETE_WINDOW", self.close_top)
# Make sure this dialog stays on top of the root console
self.top.transient(self)
# Make this dialog positioned in the center of the screen
self.update_idletasks()
win_width = self.top.winfo_width()
win_height = self.top.winfo_height()
x = self.winfo_screenwidth() // 2 - win_width // 2
y = self.winfo_screenheight() // 2 - win_height // 2
self.top.geometry('{}x{}+{}+{}'.format(win_width, win_height, x, y))
# set state to next state and exit
self.state = 2
self.executive()
# waiting for the accept button to be pushed.
if self.text_accepted:
self.state = 3elif self.abort_selected:
self.executive()
self.state = 4else:
self.executive()
self.after(100,self.executive)
elif (self.state == 3):
# Do something with the input (boring example).elif (self.state == 4):
print self.mytext
self.state = 4
self.executive()
# Exit state. Call the built-in .quit() function to exit the GUI
# Note that you can still call a built-in MessageBox widget any time
tkMessageBox.showinfo(title="Example complete",message="Example complete. Click OK to
exit.")
self.quit()
# Note that this "start" function is basically redundant, however having
# a function with this name helps readability.
def start(self):
self.state = 0;
self.executive()
# Main code for example
app = mygui()
app.start()
app.mainloop()
app.destroy()