CS2021- Week 7 Tkinter Tour. Core tkinter concepts Widget Classes: Label Button Frame Toplevel, Tk Message, Entry, Checkbutton, Radiobutton Menubutton,

Slides:



Advertisements
Similar presentations
Tk. Toolkit n Wish - windowing shell –touch fileName –chmod +x fileName –xedit fileName & –#!/usr/local/bin/wish n Widgets - eg Buttons, Labels, Frames.
Advertisements

Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books.
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Benchmark Series Microsoft Access 2010 Level 1
Chapter 1: An Introduction to Visual Basic 2012
Graphical User Interfaces
Graphical User Interface (GUI) A GUI allows user to interact with a program visually. GUIs are built from GUI components. A GUI component is an object.
Moving data on the worksheet Copying and Pasting Data, Slide 1Copyright © 2004, Jim Schwab, University of Texas at Austin Like other windows applications,
MS-Access XP Lesson 5. Creating a Query with Expression Builder Eg. Consider the following table. Table Name: Overtime Fields & Data types: Emp No (Number),
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.
Programming a GUI Hanan sedaghat pisheh. For calling GUI, we need a function with no inputs or outputs First We create a m.file m file has the same name.
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)
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.
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lab 6: event & input intro User Interface Lab: GUI Lab Oct. 2 nd, 2013.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Graphical User Interface Components: Part 1 Outline 10.1 Introduction 10.2 Tkinter Overview 10.3.
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
PYTHON GUI PROGRAMMING
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.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
© 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.
Objects and Classes Procedural Programming A series of functions performing specific tasks Data items are passed from one function to another by arguments.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
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.
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Creating visual interfaces in python
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
HTML Concepts and Techniques Fifth Edition Chapter 3 Creating Web Pages with Links, Images, and Formatted Text.
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.
See Winter 2016CISC101 - Prof. McLeod1.
INTRO2CS Tirgul 13 – More GUI and Optimization. Today:  GUI  Packing  Events  Canvas  OptionMenu  Optimization  Traveling Sales Man  Questions.
Programming with Microsoft Visual Basic 2012 Chapter 1: An Introduction to Visual Basic 2012.
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
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)
Microsoft Excel.
CISC101 Reminders Grading of Quiz 4 underway.
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.
Graphical User Interfaces
Fundamentals of Programming I Windows, Labels, and Command Buttons
Computer Science 111 Fundamentals of Programming I Text Areas
Let’s begin our game!.
Topics Graphical User Interfaces Using the tkinter Module
Python – Tkinter Windows Application
Tkinter Event. אירוע. Events and Bindings widget.bind(event, handler)
The University of Texas – Pan American
Tkinter User Input Form
Presentation transcript:

CS2021- Week 7 Tkinter Tour

Core tkinter concepts Widget Classes: Label Button Frame Toplevel, Tk Message, Entry, Checkbutton, Radiobutton Menubutton, Scrollbar, Listbox Canvas Composites: Dialog, ScrolledText, OptionMenu

Example 8-3. PP4E/Gui/Tour/toplevel0.py #independent windows part of same process import sys from tkinter import Toplevel, Button, Label win1 = Toplevel() # two independent windows win2 = Toplevel() # but part of same process Button(win1, text='Spam', command=sys.exit).pack() Button(win2, text='SPAM', command=sys.exit).pack() Label(text='Popups').pack() # on default Tk() root window win1.mainloop()

Top-Level Window Protocols toplevel2.py from tkinter import * root = Tk() # explicit root trees = [('The Larch!', 'light blue'), ('The Pine!', 'light green'), ('The Giant Redwood!', 'red')] for (tree, color) in trees: win = Toplevel(root) # new window win.title('Sing...') # set border win.protocol('WM_DELETE_WINDOW', lambda:None) # ignore close win.iconbitmap('py-blue-trans-out.ico') # not red Tk msg = Button(win, text=tree, command=win.destroy) # kills one win msg.pack(expand=YES, fill=BOTH) msg.config(padx=10, pady=10, bd=10, relief=RAISED) msg.config(bg='black', fg=color, font=('times', 30, 'bold italic')) root.title('Lumberjack demo') Label(root, text='Main window', width=30).pack() Button(root, text='Quit All', command=root.quit).pack() # kills all app root.mainloop()

Standard dialogs generate—file selection dialogs, error and warning pop ups, and question and answer prompts.

“Smart Quit” quitter.py from tkinter import * # get widget classes from tkinter.messagebox import askokcancel # get canned std dialog class Quitter(Frame): # subclass our GUI def __init__(self, parent=None): # constructor method Frame.__init__(self, parent) self.pack() widget = Button(self, text='Quit', command=self.quit) widget.pack(side=LEFT, expand=YES, fill=BOTH) def quit(self): ans = askokcancel('Verify exit', "Really quit?") if ans: Frame.quit(self) if __name__ == '__main__': Quitter().mainloop()

Creating dialogs #More useful than priniting to stdout # define a name:callback demos table from tkinter.filedialog import askopenfilename # get standard dialogs from tkinter.colorchooser import askcolor # they live in Lib\tkinter from tkinter.messagebox import askquestion, showerror from tkinter.simpledialog import askfloat demos = { 'Open': askopenfilename, 'Color': askcolor, 'Query': lambda: askquestion('Warning', 'You typed "rm *"\nConfirm?'), 'Error': lambda: showerror('Error!', "He's dead, Jim"), 'Input': lambda: askfloat('Entry', 'Enter credit card number') }

dialogTable.py demoDlg.py from tkinter import * # get base widget set from dialogTable import demos # button callback handlers from quitter import Quitter # attach a quit object to me class Demo(Frame): def __init__(self, parent=None, **options): Frame.__init__(self, parent, **options) self.pack() Label(self, text="Basic demos").pack() for (key, value) in demos.items(): Button(self, text=key, command=value).pack(side=TOP, fill=BOTH) Quitter(self).pack(side=TOP, fill=BOTH) if __name__ == '__main__': Demo().mainloop()

Printing dialog results demoDlg-print.py from tkinter import * # get base widget set from dialogTable import demos # button callback handlers from quitter import Quitter # attach a quit object to me class Demo(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack() Label(self, text="Basic demos").pack() for key in demos: func = (lambda key=key: self.printit(key)) Button(self, text=key, command=func).pack(side=TOP, fill=BOTH) Quitter(self).pack(side=TOP, fill=BOTH) def printit(self, name): print(name, 'returns =>', demos[name]()) # fetch, call, print if __name__ == '__main__': Demo().mainloop()

Enclosing scope Lookup # Button-press callbacks are run with no arguments # Actual handler printit needs extra data (i.e. demo key) # must wrap the handler with lambda, enclosing scope lookup for key in demos: func = (lambda key=key: self.printit(key)) Button(self, text=key, command=func).pack(side=TOP, fill=BOTH) # for loop creates new scope for key so use default argument to pass the current key to the printit handler # the lambda remembers and passes on the state information

Binding Events – Tour/bind.py Callback functions triggered when bound events occur. Callbacks for bind receive Event object argument, an instance of tkinter Event class from tkinter import * def showPosEvent(event): print('Widget=%s X=%s Y=%s' % (event.widget, event.x, event.y)) def showAllEvent(event): print(event) for attr in dir(event): if not attr.startswith('__'): print(attr, '=>', getattr(event, attr)) def onKeyPress(event): print('Got key press:', event.char) def onArrowKey(event): print('Got up arrow key press')

tkroot = Tk() labelfont = ('courier', 20, 'bold') # family, size, style widget = Label(tkroot, text='Hello bind world') widget.config(bg='red', font=labelfont) # red background, large font widget.config(height=5, width=20) # initial size: lines,chars widget.pack(expand=YES, fill=BOTH) widget.bind(' ', onLeftClick) # mouse button clicks widget.bind(' ', onRightClick) widget.bind(' ', onMiddleClick) # middle=both on some mice widget.bind(' ', onDoubleLeftClick) # click left twice widget.bind(' ', onLeftDrag) # click left and move widget.bind(' ', onKeyPress) # all keyboard presses widget.bind(' ', onArrowKey) # arrow button pressed widget.bind(' ', onReturnKey) # return/enter key pressed widget.focus() # or bind keypress to tkroot tkroot.title('Click Me') tkroot.mainloop()

Tour/canvasDraw.py class CanvasEventsDemo: def __init__(self, parent=None): canvas = Canvas(width=300, height=300, bg='beige') canvas.pack() canvas.bind(' ', self.onStart) # click canvas.bind(' ', self.onGrow) # and drag canvas.bind(' ', self.onClear) # delete all canvas.bind(' ', self.onMove) # move latest self.canvas = canvas self.drawn = None self.kinds = [canvas.create_oval, canvas.create_rectangle] def onStart(self, event): self.shape = self.kinds[0] self.kinds = self.kinds[1:] + self.kinds[:1] # start dragout self.start = event self.drawn = None

Binding to specific items canvas-bind.py from tkinter import * def onCanvasClick(event): print('Got canvas click', event.x, event.y, event.widget) def onObjectClick(event): print('Got object click', event.x, event.y, event.widget, end=' ') print(event.widget.find_closest(event.x, event.y)) # find text object's ID root = Tk() canv = Canvas(root, width=100, height=100) obj1 = canv.create_text(50, 30, text='Click me one') obj2 = canv.create_text(50, 70, text='Click me two') canv.bind(' ', onCanvasClick) # bind to whole canvas canv.tag_bind(obj1, ' ', onObjectClick) # bind to drawn item canv.tag_bind(obj2, ' ', onObjectClick) # a tag works here too canv.pack() root.mainloop()

Homework #7 Two week design project: Due Wed Oct 21 Developing a GUI to meet Business need Define a business information need – Enumerate features through “user stories” Create a GUI that addresses that need Your solution should include buttons and dialogs Free to explore code samples in PP4E/Gui