Presentation is loading. Please wait.

Presentation is loading. Please wait.

PYTHON GUI PROGRAMMING

Similar presentations


Presentation on theme: "PYTHON GUI PROGRAMMING"— Presentation transcript:

1 PYTHON GUI PROGRAMMING
1

2 Python provides various options for developing graphical user interfaces (GUIs).
Tkinter: Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. (originally developed for the Tcl language) Tkinter is an open source, portable graphical user interface (GUI) toolkit designed for use in Python scripts.

3 Tkinter Programming #/usr/bin/python import Tkinter top =Tkinter.Tk()
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps: Example:Import the Tkinter module. Create the GUI application main window. Add one or more of the widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user. #/usr/bin/python import Tkinter top =Tkinter.Tk() # Code to add widgets will go here... top.mainloop()

4 mainloop

5 Tkinter widgets Tkinter’s components are called Widgets. Widgets are equivalents to OOP’s objects or components All widgets inherit from the Widget class Tkinter provides various controls, such as buttons, labels, and text boxes, used in a GUI application. These controls are commonly called widgets. Geometry Management: All Tkinter widgets have access to specific geometry management methods, which have the purpose of organizing widgets throughout the parent widget area. Tkinter exposes the following geometry manager classes: pack, grid, and place. The pack() Method - This geometry manager organizes widgets in blocks before placing them in the parent widget. The grid() Method - This geometry manager organizes widgets in a table-like structure in the parent widget. The place() Method -This geometry manager organizes widgets by placing them in a specific position in the parent widget. 5

6 Tkinter button Python - Tkinter Button
The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button, which is called automatically when you click the button. Syntax: w = Button ( master, option=value, ... ) Parameters: master: This represents the parent window. options: There are many options for this widget. These options can be used as key-value pairs separated by commas.

7 Button example #!/usr/bin/python import Tkinter import tkMessageBox
top = Tkinter.Tk() def hello(): tkMessageBox.showinfo(“Hello", "Hello World") B1 = Tkinter.Button(top, text = “Hello", command = hello) label= Tkinter.Label(top,text="Hello World!") B1.pack() label.pack() top.mainloop()

8 Tkinter canvas The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets, or frames on a Canvas. arc . Creates an arc item. coord = 10, 50, 240, 210 arc = canvas.create_arc(coord, start=0, extent=150, fill="blue") Image:Creates an image item, which can be an instance of either the Bitmap Image or the PhotoImage classes. filename = PhotoImage(file = "sunshine.gif") image = canvas.create_image(50, 50, anchor=NE, image=filename) line . Creates a line item. line = canvas.create_line(x0, y0, x1, y1,..., xn, yn, options) oval . Creates a circle or an ellipse at the given coordinates. oval = canvas.create_oval(x0, y0, x1, y1, options) polygon . Creates a polygon item that must have at least three vertices. oval = canvas.create_polygon(x0, y0, x1, y1,..xn, yn, options)

9 Tkinter canvas example
import Tkinter top = Tkinter.Tk() C = Tkinter.Canvas(top, bg="blue", height=250, width=300) coord = 10, 50, 240, 210 arc = C.create_arc(coord, start=0, extent=150, fill="red") C.pack() top.mainloop()

10 Tkinter check box from Tkinter import * import Tkinter
top = Tkinter.Tk() CheckVar1 = IntVar() CheckVar2 = IntVar() C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \ onvalue = 1, offvalue = 0, height=5, width = 20) C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \ C1.pack(side=Tkinter.LEFT) C2.pack() top.mainloop() if (CheckVar1.get() == 1): print ("Music selected") The get method returns the current value of the variable, as a Python object. 

11 Tkenter entry widget Python - Tkinter Entry:
The Entry widget is used to accept single- line text strings from a user. If you want to display multiple lines of text that can be edited, then you should use the Text widget. If you want to display one or more lines of text that cannot be modified by the user then you should use the Label widget.

12 L1 = Label(root, text="User Name") L1.pack( side = LEFT)
import Tkinter from Tkinter import * root = Tk() root.title('Random') L1 = Label(root, text="User Name") L1.pack( side = LEFT) E1 = Entry(root, width=10) E1.pack(side=TOP,padx=10,pady=10) def onok(): x = E1.get() print(x) Button(root, text='OK', command=onok).pack(side=LEFT) root.mainloop()

13 Example from Tkinter import * import Tkinter class App:
def __init__(self, master): frame = Frame(master) frame.pack() self.button = Button(frame, text="QUIT", fg="red", command=frame.quit) self.button.pack(side=LEFT) self.hi_there = Button(frame, text="Hello", command=self.say_hi) self.hi_there.pack(side=LEFT) def say_hi(self): print ("hi there, everyone!") root = Tk() app = App(root) root.mainloop()

14 Events and Bindings Events can come from various sources, including key presses and mouse operations by the user, and redraw events from the window manager. For each widget, you can bind Python functions and methods to events. widget.bind(event, handler) If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.

15 Example from Tkinter import * root = Tk() def key(event):
print ("pressed", repr(event.char)) def pushb(event): frame.focus_set() print ("clicked at", event.x, event.y) frame = Frame(root, width=100, height=100) frame.bind("<Key>", key) frame.bind("<Button-1>", pushb) frame.pack() root.mainloop()

16 Example-2 import Tkinter import ttk class Application:
def __init__(self, root): self.root = root self.root.title('Button Demo') ttk.Frame(self.root, width=250, height=100).pack() self.init_widgets() def init_widgets(self): ttk.Button(self.root, command=self.insert_txt, text='Click Me', width='10').place(x=10, y=10) self.txt = Tkinter.Text(self.root, width='15', height='2') self.txt.place(x=10, y=50) def insert_txt(self): self.txt.insert(Tkinter.INSERT, 'Hello World\n') if __name__ == '__main__': root = Tkinter.Tk() Application(root) root.mainloop()

17 Example - menu from Tkinter import * def callback():
print ("called the callback!") root = Tk() # create a menu menu = Menu(root) root.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=callback) filemenu.add_command(label="Open...", command=callback) filemenu.add_separator() filemenu.add_command(label="Exit", command=callback) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=callback) root.mainloop()

18 Text Area from Tkinter import * class mywidgets:
def __init__(self,root): frame=Frame(root) frame.pack() self.txtfr(frame) return def txtfr(self,frame): #define a new frame and put a text area in it textfr=Frame(frame) self.text=Text(textfr,height=10,width=50,background='white') # put a scroll bar in the frame scroll=Scrollbar(textfr) self.text.configure(yscrollcommand=scroll.set) #pack everything self.text.pack(side=LEFT) scroll.pack(side=RIGHT,fill=Y) textfr.pack(side=TOP) def main(): root = Tk() s=mywidgets(root) root.title('textarea') root.mainloop() main()

19 ttk progress bar example
import sys import ttk from Tkinter import * mGui = Tk() mGui.geometry('450x450') mGui.title('Hanix Downloader') mpb = ttk.Progressbar(mGui,orient="horizontal",length = 200, mode ="determinate") mpb.pack() mpb["maximum"] = 100 mpb["value"] = 50 mGui.mainloop()

20 Exercises Write a dialog-style application that calculates compound interest. The application should be very similar in style and structure to the Currency application, and should look like this: Write a program that creates a Canvas and a Button. When the user presses the Button, it should draw a circle on the canvas. Write a program that creates a GUI with a single button. When the button is pressed it should create a second button. When that button is pressed, it should create a label that says, “Nice job!”.


Download ppt "PYTHON GUI PROGRAMMING"

Similar presentations


Ads by Google