PYTHON GUI PROGRAMMING

Slides:



Advertisements
Similar presentations
Topics in Python Blackjack & TKinter
Advertisements

Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Creating an OOED Application
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Customizing Outlook. Forms Window in which you enter and view information in Outlook Outlook Form Designer The environment in which you create and customize.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Chapter 3 Working with Symbols and Interactivity.
Getting started (For the absolute beginner: download and install python) Task 1: Creating 3 widgets: a label, text entry and a button (and ‘PACK’) Task.
WEEK Introduction to GUI programming. Introduction Each data type can represent a certain set of values, and each had a set of associated operations.
COMPSCI 101 Principles of Programming
18. Python - GUI Programming (Tkinter)
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Tcl/Tk package in R Yichen Qin
Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools.
The University of Texas – Pan American
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
© 2011 Delmar, Cengage Learning Chapter 3 Working with Symbols and Interactivity.
Working with Symbols and Interactivity
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
XP Tutorial 4 Special Animations. XP New Perspectives on Macromedia Flash MX Special Layers for Animation Animation Using a Motion Guide Layer.
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Lecture 10: Toolkits: Intrinsics, Callbacks, Resources, Widget hierarchies, Geometry management Brad Myers Advanced User Interface Software 1© 2013.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Tkinter Canvas.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Objects and Classes Procedural Programming A series of functions performing specific tasks Data items are passed from one function to another by arguments.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Tkinter Basics. Initial Stuff To get the package: from tkinter import * To make a window, give it a title and operate it: windowVar = Tk() windowVar.title(“your.
Lesson 3 - The Entry Box.. Last week We produced a Tkinter window with a label and a button Now we are going to create a new program that creates a Button.
9-Nov-97Tri-Ada '971 TASH An Alternative to the Windows API TRI-Ada ‘97 Terry J. Westley
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Creating visual interfaces in python
Guide to Programming with Python
COMPSA Exam Prep Session by Paul Allison On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
CS2021- Week 7 Tkinter Tour. Core tkinter concepts Widget Classes: Label Button Frame Toplevel, Tk Message, Entry, Checkbutton, Radiobutton Menubutton,
1 Chapter 6: Creating Oracle Data Block Forms. 2 Forms  Application with a graphical user interface that looks like a paper form  Used to insert, update,
Using TKINTER For Better Graphic Tkinter module offers more functions: Button Widget: from tkinter import * tk =Tk() btn=Button(tk, text=‘Click me’) btn.pack()
Topics Graphical User Interfaces Using the tkinter Module
Graphical User Interfaces (GUIs)
Chapter 1: An Introduction to Visual Basic 2015
Chapter 21 – Graphics/Tk Outline 21.1 Introduction 21.2 GD Module: Creating Simple Shapes 21.3 GD Module: Image Manipulation 21.4 Chart Module 21.5 Introduction.
Graphical User Interfaces (GUIs)
GUI Using Python.
Fundamentals of Python: From First Programs Through Data Structures
Tkinter Python User Interface
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Tkinter Widgets CS 260 Dick Steflik.
TKinter CS-360 Dick Steflik.
I210 review.
Working with Symbols and Interactivity
Fundamentals of Programming I Windows, Labels, and Command Buttons
Topics Graphical User Interfaces Using the tkinter Module
Constructors, GUI’s(Using Swing) and ActionListner
Python – Tkinter Windows Application
The University of Texas – Pan American
Presentation transcript:

PYTHON GUI PROGRAMMING 1

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.

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()

mainloop

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

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.

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()

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)

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()

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. 

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.

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()

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()

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.

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()

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()

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()

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()

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()

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!”.