#!/usr/bin/python from Tkinter import * import tkMessageBox class StatusBar(Frame): def __init__(self, master): Frame.__init__(self, master) self.label = Label(self, text="", bd=1, relief=SUNKEN, anchor=W) self.label.pack(side=BOTTOM, fill=X) def set(self, format, *args): self.label.config(text=format % args) self.label.update_idletasks() def clear(self): self.label.config(text="") self.label.update_idletasks() class App: def __init__(self, parent): self.this = self frame = Frame(parent) parent.protocol("WM_DELETE_WINDOW", ConfirmQuit) parent.geometry("%dx%d%+d%+d" % (600, 400, 100, 50)) # create a menu menu = Menu(parent) parent.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=self.newFile) filemenu.add_command(label="Open...", command=self.OpenFile) filemenu.add_separator() filemenu.add_command(label="Exit", command=self.Exit) runmenu = Menu(menu) menu.add_cascade(label="Run", menu=runmenu) runmenu.add_command(label="Run now", command=self.callback) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=self.About) def callback(event): print "callback was called from event class ",event.__class__ print "event.__name__=",__name__ print "event type:", type(event) print "event.this.__class__=", event.this.__class__ print "this: type=", type(event.this), ",class=", event.this.__class__ print "status_bar", status_bar.__class__ status_bar.set("%s", "Ahlan bib") def newFile(event): print "New file", event status_bar.set("%s", "Creating a new file") def OpenFile(event): print "Open File", event status_bar.set("%s", "Opening a file") def About(event): print "(c) 2012, mlutfi", event status_bar.clear() def say_hi(self): print "hi there, everyone!" def Exit(event): ConfirmQuit() class BaseWindow: def __init__(self, parent): Toplevel() def ConfirmQuit(): if tkMessageBox.askokcancel("Quit", "Do you really want to quit?"): root.destroy() root = Tk() root.protocol("WM_DELETE_WINDOW", ConfirmQuit) status_bar = StatusBar(root) status_bar.pack(side=BOTTOM, fill=X) app = App(root) root.mainloop()
Wednesday, March 14, 2012
Python GUI
This small Python script creates a window application and handles menu events as well as updating statusbar at the bottom.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment