#!/usr/bin/python #tkex1.py - tkDemo#1 1.1 - 30 May 01 #A Windows/Unix script to demonstrate basic Tkinter programming #Update at http://www.3dartist.com/WP/python/tknotes.htm#tkex1 # Also see tkex1x.py for Win/Unix/Mac #Created by Bill Allen #HOW TO USE: #This script does nothing except demo a basic graphic interface. #This is not a self-executing program but rather a Python script. # You don't have to be a programmer to use it, but you must have # the Python 2.x language interpreter installed on your system. # Python is a free download from http://www.python.org for many # different platforms (Mac, Win95+, etc.), and comes with Linux # (Linux users may need to upgrade to 2.x). #Place this script anywhere you like on a machine with Python # already installed, then either click on the script like any # regular program, or launch it from the OS or Python command line, # or from within the IDLE interactive Python programming environment. #NOTE: To run safely and well from IDLE or any other Tkinter-based IDE, # 1) set usingIDLE to = 1 # 2) you may have to close the Python shell window (not the script # window) between runnings of this script #For more about Tkinter scripting, visit # http://www.3dartist.com/WP/python/tknotes.htm #PLATFORM: This code should run on any machine that has Python # 2.x installed, but has been tested only with 2.0 on Win98/ME. #HISTORY: #2001-05-30: v1.1 updated for safer handling #2001-02-20: v1.0 first posted #LICENSE: This code is free software provided as is and without # warranty for fitness for any purpose. Use it at your own risk. # This code and derivations may be used anywhere, including for # commercial application. If used publicly, such as with another # teaching document, acknowledgment and a link would be appreciated. ### BEGIN PROGRAM ### #--- user variable --- usingIDLE = 0 #set = 1 if running from IDLE, otherwise 0 #--- modules --- import tkMessageBox, tkSimpleDialog import Tkinter as tk #--- classes --- class uiClass: #user interface class def __init__(self,master,ar,xy,flex): # used once for main GUI menubar = tk.Menu(master) #create a menu object tied master.config(menu=menubar) # to the root parent mainmenu = tk.Menu(menubar,tearoff=0) #create a dropdown menu mainmenu.add_command(label='Dialog', #add first menu item underline=0, #keyboard shortcut = "D" command=self.sayWhat) #what to do when called mainmenu.add_command(label='Exit',underline=1,command=self.quit) menubar.add_cascade(label='Main', #give the dropdown menu a menu=mainmenu, # name & add it to the underline=0) # menu object self.frame = tk.Toplevel(relief='ridge', #border style borderwidth=2, #create the main GUI window menu=menubar) # with its menu object self.frame.geometry(ar+xy) #set its size & location self.frame.resizable(flex,flex) #(dis)allow resize horiz/vert if not usingIDLE: #system window exit protocol self.frame.protocol('WM_DELETE_WINDOW',self.quit) #end def self.__init__ def quit(self): if usingIDLE: self.frame.destroy() else: self.frame.quit() root.quit() def sayWhat(self): t = tkSimpleDialog.askstring('Input', #dialog title 'Type something here:', #prompt parent=self.frame) #ALWAYS have this if t: tkMessageBox.showinfo('You wrote:', '\"'+t+'\"') #end define uiClass #--- do some work --- if __name__ == '__main__': #optional, but good practice root = tk.Tk() root.title('tkDemo#1') root.withdraw() #suppress unwanted window mainUI = uiClass(root, #set up the main GUI window '400x200', # width & height '+20+20' # initial XY screen location ,0) # resizing turned off if not usingIDLE: #avoid IDLE or other Tkinter IDEs root.mainloop() #outer event loop