python programming - xiii. gui programming

Download Python Programming - XIII. GUI Programming

Post on 10-May-2015

2.384 views

Category:

Technology

16 download

Embed Size (px)

TRANSCRIPT

  • 1.Engr. RANEL O. PADON XIII. GUI PROGRAMMING

2. PYTHON PROGRAMMING TOPICS I Introduction to Python Programming II Python Basics III Controlling the Program Flow IV Program Components: Functions, Classes, Packages, and Modules V Sequences (List and Tuples), and Dictionaries VI Object-Based Programming: Classes and Objects VII Customizing Classes and Operator Overloading VIII Object-Oriented Programming: Inheritance and Polymorphism IX Randomization Algorithms X Exception Handling and Assertions XI String Manipulation and Regular Expressions XII File Handling and Processing XIII GUI Programming Using Tkinter 3. PYTHON GUI: TKINTER Python Operating System 4. PYTHON GUI: TKINTER Python Tcl Tk Operating System 5. PYTHON GUI: TKINTER Python Tcl Tk Operating System Tkinter 6. PYTHON GUI: TKINTER Tkinter Operating System 7. PYTHON GUI: TKINTER Tkinter = Tcl + Tk 8. PYTHON GUI: TKINTER = Tcl + Tk Tcl: Tool Command Language invented in 1990 used for web and desktop applications, networking, administration, scripted applications, embedded system, rapid prototyping and testing. 9. PYTHON GUI: TKINTER = Tcl + Tk Tcl: Tool Command Language Interfacing with other Languages Tcl/C++ Tcl/Java 10. PYTHON GUI: TKINTER = Tcl + Tk Tcl: Tool Command Language allows extension packages, which provide additional functionality, such as a GUI, terminal-based application automation, database access, etc. Tcl/Tk Tcl/Expect 11. PYTHON GUI: TKINTER = Tcl + Tk Tk: ToolKit open-source, cross platform widget toolkit for building GUI other common widgets are: * Java AWT and Java Swing (OpenOffice) * Qt (Google Earth, Skype, Linux KDE windows) * GTK+ (Linux Gnome windows) * Cocoa (Mac OS) * Silverlight (modern MS Windows NT) 12. PYTHON GUI: TKINTER = Tcl + Tk Tk: ToolKit created originally for Tcl other language bindings also exist: * Python/Tk = Tkinter * Perl/Tk * Ruby/Tk 13. PYTHON GUI: TKINTER = Tcl + Tk Tkinter: Tk Interface * wrapper for Tcl to access Tk * Python and Tcl could co-exist in a Python file. * Python interpreter also contains Tcl and Tk interpreter 14. PYTHON GUI: TKINTER Related Projects Language Widget Toolkit Java Abstract Windowing Toolkit Swing PHP PHP - GTK PHP - Qt Javascript jQuery UI ExtJS Python Tkinter Pyjamas PyGTK PyQt PySide wxPython 15. PYTHON GUI: TKINTER Related Projects (Javas Swing with Nimbus Theme) 16. PYTHON GUI: TKINTER = Tcl + Tk 17. PYTHON GUI: TKINTER = Tcl + Tk Tk A GUI toolkit provided as a library of C routines. This library manages and manipulates the windows and handles the GUI events and user interaction. Tcl The (mostly hidden) language used by Tk and, hence, used by Tkinter to communicate with the Tk toolkit. Tkinter The Python Tk interface. A Python module that provides a collection of Python classes and methods for accessing the Tk toolkit from within Python. 18. PYTHON GUI: TKINTER Widget A user interface element (text box, combo box, or top-level window). On Windows, the common terminology is control or window. 19. PYTHON GUI: Pros Brevity Python programs using Tkinter can be very brief, partly because of the power of Python, but also due to Tk. Reasonable default values are defined for many options used in creating a widget, and packing it (i.e., placing and displaying). 20. PYTHON GUI: Pros Cross platform Tk provides widgets on Windows, Macs, and most Unix implementations with very little platform-specific dependence. Some newer GUI frameworks are achieving a degree of platform independence, but it will be some time before they match Tk's in this respect. 21. PYTHON GUI: Pros Maturity First released in 1990, the core is well developed and stable. 22. PYTHON GUI: Pros Extensibility Many extensions of Tk exist, and more are being frequently distributed on the Web. Any extension is immediately accessible from Tkinter, if not through an extension to Tkinter, then at least through Tkinter's access to the Tcl language. 23. PYTHON GUI: Cons Speed Most calls to Tkinter are formatted as a Tcl command (a string) and interpreted by Tcl from where the actual Tk calls are made. This theoretical slowdown caused by the successive execution of two interpreted languages is rarely seen in practice and most real-world applications spend little time communicating between the various levels of Python, Tcl, and Tk. 24. PYTHON GUI: Cons Tcl Python purists often balk at the need to install another scripting language in order to perform GUI tasks. There is periodic interest in removing the need for Tcl by using Tk's C-language API directly, although no such attempt has ever succeeded. 25. PYTHON GUI: TKINTER Python Widget (Window Gadget) Subclasses 26. PYTHON GUI: TKINTER Python Widgets 27. from Tkinter import * Frame().mainloop() PYTHON GUI: Frame 28. from Tkinter import * class GuiTest(Frame): def __init__(self): Frame.__init__(self) self.pack() GuiTest().mainloop() PYTHON GUI: Frame 29. from Tkinter import * class GuiTest(Frame): def __init__(self): Frame.__init__(self) self.pack() self.master.title("Ranel's Lugawan") GuiTest().mainloop() PYTHON GUI: Frame 30. from Tkinter import * class GuiTest(Frame): def __init__(self): Frame.__init__(self) self.pack() self.master.title("Ranel's Lugawan") self.label = Label(self, text = "Ako ang Simula") self.label.pack() GuiTest().mainloop() PYTHON GUI: Label 31. from Tkinter import * class GuiTest(Frame): def __init__(self): self.text = Entry(self, name = butas) self.text.pack() GuiTest().mainloop() PYTHON GUI: Entry 32. from Tkinter import * class GuiTest(Frame): def __init__(self): self.text = Entry(self, name = butas) self.text.bind("", self.anoLaman) self.text.pack() def anoLaman(self, propeta): print "ang laman ay", propeta.widget.get() PYTHON GUI: Event Binding 33. from Tkinter import * from tkMessageBox import * class GuiTest(Frame): def __init__(self): self.text = Entry(self, name = butas") self.text.bind("", self.anoLaman) self.text.pack() def anoLaman(self, propeta): showinfo() PYTHON GUI: Message Box 34. from Tkinter import * from tkMessageBox import * class GuiTest(Frame): def __init__(self): self.text = Entry(self, name = butas") self.text.bind("", self.anoLaman) self.text.pack() def anoLaman(self, propeta): pangalan = propeta.widget.winfo_name() laman = propeta.widget.get() showinfo("Message", pangalan+" : "+laman) PYTHON GUI: Message Box 35. from Tkinter import * from tkMessageBox import * class GuiTest(Frame): def __init__(self): self.text = Entry(self, name = butas") self.text.bind("", self.anoLaman) self.text.insert(INSERT, Ano ang misyon mo?") self.text.pack() def anoLaman(self, propeta): pangalan = propeta.widget.winfo_name() laman = propeta.widget.get() showinfo("Message", pangalan + " : " + laman) PYTHON GUI: Entry Insert Function 36. from Tkinter import * from tkMessageBox import * class GuiTest(Frame): def __init__(self): self.button = Button(self, text = "Koya wag po!", command = self.pinindot) self.button.pack() def pinindot(self): showinfo("Mensahe", "Bakit nyo ako pinindot? :(") PYTHON GUI: Button 37. from Tkinter import * class GuiTest(Frame): def __init__(self): self.button = Button(self, text = "Koya wag po!", command = self.pinindot) self.button.bind("", self.iLubog) self.button.bind("", self.iAngat) self.button.pack() def iLubog(self, propeta): propeta.widget.config(relief = GROOVE) def iAngat(self, propeta): propeta.widget.config(relief = RAISED) PYTHON GUI: Button Hover 38. class GuiTest(Frame): def __init__(self): self.imageGoogle = PhotoImage(file = "google.gif") self.buttonPic = Button(self, image = self.imageGoogle, command = self.pinindotPiktyur) self.buttonPic.bind("", self.iLubog) self.buttonPic.bind("", self.iAngat) self.buttonPic.pack() def pinindotPiktyur(self): showinfo("Mensahe", Di pa po kayo bayad!") PYTHON GUI: Button with PhotoImage 39. PYTHON GUI: Radiobutton 40. PYTHON GUI: Radiobutton from Tkinter import * class ChannelButton(Frame): ChannelButton().mainloop() 41. def __init__(self): Frame.__init__(self) self.master.title("Radiobutton Demo") self.master.geometry("250x60") self.pack(expand = YES, fill = BOTH) self.laman = Entry(self, width = 60, justify = CENTER) self.laman.insert(INSERT, "Ano ang paborito mongChannel?") self.laman.config(state = DISABLED) self.laman.pack(padx = 5, pady = 5) PYTHON GUI: Radiobutton 42. def __init__(self): listahanNgChannels = ["GMA","ABS-CBN","TV-5","DZUP"] self.napilingChannel = StringVar() self.napilingChannel.set("DZUP") for istasyon in listahanNgChannels: isangButones = Radiobutton(self, text = istasyon, value = istasyon, variable = self.napilingChannel, command = self.ipakitaAngNapili) isangButones.pack(side=LEFT, expand=1, fill=BOTH) PYTHON GUI: Radiobutton 43. def __init__(self): def ipakitaAngNapili(self): print self.napilingChannel.get() if self.napilingChannel.get() == "GMA": showinfo("Tagline", "Kapuso") elif self.napilingChannel.get() == "ABS-CBN": showinfo("Tagline", "Kapamilya") elif self.napilingChannel.get() == "TV-5": showinfo("Tagline", "Kapatid") else: showinfo("Tagline", "Itanong Mo Kay Engr:Every Monday @ 4-5pm!") PYTHON GUI: Checkbutton 44. PYTHON GUI: Checkbutton 45. from Tkinter import * class CheckbuttonTest(Frame): CheckbuttonTest().mainloop() PYTHON GUI: Radiobutton 46. def __init__(self): Frame.__init__(self) self.master.title("Check Box Demo") self.master.geometry("200x50") self.pack(expand = YES, fill = BOTH) self.laman = Entry(self, width = 20,font = "Arial 10", justify = CENTER) self.laman.insert(INSERT, "Tagilid at Makapal") self.laman.pack(padx = 5, pady = 5) PYTHON GUI: Radiobutton 47. def __init__(self): self.makapalBa = BooleanVar() self.tsekbaksMakapal = Checkbutton(self, text =Pakapalin, variable = self.makapalBa, command = self.baguhinAngFont) self.tsekbaksMakapal.pack(side = LEFT, expand = YES, fill = X) PYTHON GUI: Checkbutton 48. def __init__(self): self.tagilidBa = BooleanVar() self.tsekbaksTagilid = Checkbutton(self, text ="Itagilid", variable = self.tagili

Recommended

View more >