cisc101 reminders notes today
play

CISC101 Reminders & Notes Today Test 3 is being graded From - PowerPoint PPT Presentation

CISC101 Reminders & Notes Today Test 3 is being graded From last time Marks will be posted in Moodle Insertion sort Tests will be returned in next weeks tutorial Slides 28-30 Bubble Sort GUIs with


  1. CISC101 Reminders & Notes Today • Test 3 is being graded • From last time … – Marks will be posted in Moodle – Insertion sort – Tests will be returned in next week’s tutorial • Slides 28-30 • Bubble Sort • GUIs with Tkinter • Will schedule exam review sessions – Date, place and time will be announced – Widgets – Placement of widgets Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Bubble Sort Bubble Sort - Cont. • Is best envisioned as a vertical column of def bubbleSort(numsList): numbers as bubbles size = len(numsList) – The larger bubbles gradually work their way to the top for i in range(0, size): – The smaller ones are pushed down to the bottom j = size - 1 while j > i : while j > i : • Loop through array from i=0 to length of array if numsList[j] < numsList[j - 1]: – Loop down from the last element in the array to i swap(numsList, j, j - 1) - Swap adjacent elements if they are in the wrong order j = j - 1 Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. Bubble Sort – A Slight Improvement Bubble Sort - Cont. def bitBetterBubbleSort(numsList): • The improvement will (potentially) reduce the size = len(numsList) number of iterations i = 0 isSorted = False • Possibly the simplest sorting algorithm to code while i < size and not isSorted: j = size - 1 • Also the slowest sorting algorithm! • Also the slowest sorting algorithm! isSorted = True isSorted = True – Assume that there are n elements while j > i : – On average, bubble sort makes n times more moves if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) than selection or insertion sort isSorted = False j = j - 1 i = i + 1 Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Timing Summary Next Slide – Quicksort • 1000 data points • You are not responsible for knowing this algorithm Sort Millisec – Between 1 and 1000 – It is included here strictly for interest Python sort() 0.62 Insertion 159 Selection 182 Bubble 450 Better?Bubble 448 Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. def quickSort(numsList, start, finish) : GUI with Tkinter lower = start + 1 upper = finish swap(numsList, start, (start + finish) / 2) • Stands for “Tk Interface” pivot = numsList[start] • A Python interface to the Tk GUI toolkit while lower <= upper : while numsList[lower] < pivot : – Maintained by ActiveState lower = lower + 1 • www.activestate.com while numsList[upper] > pivot : upper = upper - 1 • They also distribute a free Python development tool called if lower < upper: ActivePython, which is an alternative to IDLE ActivePython, which is an alternative to IDLE swap(numsList, lower, upper) • Tk consists of a bunch of components or widgets upper = upper - 1 lower = lower + 1 – Tk can be used with other languages like Perl or Ruby • See Section 24.1 in the Python Library Reference swap(numsList, upper, start) if upper - start > 1 : quickSort(numsList, start, upper - 1) if finish - upper > 1 : quickSort(numsList, upper + 1, finish) Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Some Tkinter Widgets Some Tkinter Widgets - Cont. • Button • Frame – Something to click on – Container for other widgets • Canvas • Label – To draw on or display graphics – Displays one line of text that the user cannot change • Checkbutton • Checkbutton • Listbox • Listbox – A checkbox (on or off) – Drop down list for user selection • Entry • Menu – Single line text entry – A list of choices displayed by a Menubutton Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. Some Tkinter Widgets - Cont. Using the Tkinter Module • Message • It is important to note that IDLE is programmed itself using Tkinter – Shows multiple lines of text – This might cause some strange results • Radiobutton – Does not seem to be a problem with Python 3.1 … – Appear in groups where the user can only select one • Scale • Scale • You must import the Tkinter module with one of • You must import the Tkinter module with one of – A slider import tkinter • Scrollbar from tkinter import * – Allows scrolling for other widgets • Text • You may have to install the module for this to work – User can enter multiple lines of text – For Windows Python installs it should already be there Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Using Tkinter - Cont. Using Tkinter - Cont. • Create your main or root window using • The first line top_window = tkinter.Tk() creates a Tk object top_window = tkinter.Tk() – Calls a constructor to create the object – Assigns the object to variable top_window • Then start the main loop using tkinter.mainloop() tkinter.mainloop() • The main loop is the “listener” loop for the window • The main loop is the “listener” loop for the window – It will wait very patiently for the user to do something – This assumes you used the first import statement • Demo: WindowBasic.py – Note the functionality built into even this simple window! Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. Adding a Label The “Packer” pack(side="top/right/bottom/left", expand=0/1, • Demo: WindowLabel.py anchor="n/nw/w...", fill="x/y/both") helloWorldLabel = tkinter.Label(top, text="HelloWorld!") – All of these arguments are optional helloWorldLabel.pack() • The pack() method is invoked to place the label • The “packer” is very simple but just jams stuff in into the first available position in the window into the first available position in the window – Displays each widget in its own row or column – Must use frames to get more than one widget in a single • Pretty small font … row or column – Demo: WindowLabelFont.py • We won’t be covering frames • How can we get away from this top-down stacking of widgets in the window? Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod The Grid Manager The Grid Manager - Cont. grid( row=? , column=? , rowspan=?, columnspan=?, • The Grid Manager is an alternative to the packer sticky="news", ipadx=?, ipady=?, padx=?, pady=?) – Just a bit harder to use • Specify a row and column position for widgets • "news" is North, East, West, and/or South instead • Do not mix the packer and grid together – Dictates which side the widget “sticks” to in its portion of the grid of the grid – Use one or the other! – Can set sticky to a subset of "news" • e.g. , "n“, “e“, “ws" • Other dimensions are in pixels – What is a pixel, anyways? Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  6. The Grid Manager - Cont. Widget Options • widget_name .keys() • Demo: WindowGrid.py – Gives you a list of dictionary keys – Each is an option that can be changed for that widget • Note how the columns and rows are sized to the • Any set of these keys can be set when you create width and height of the largest widget in that row the widget or column • Widgets are centre-aligned by default • Widgets are centre-aligned by default • widget_name .configure() – Change this using the sticky option – Gives you the dictionary with keys and values • They can span multiple rows and columns – Use rowspan or columnspan (or both!) • Add extra “padding” using padx and pady Winter 2011 CISC101 - Whittaker 21 Winter 2011 CISC101 - Whittaker 22 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Widget Options - Cont. Button Widget • widget_name .cget( option ) • A button allows the user to initiate an event – Returns the value of the specified option as a string – Most often to invoke some function in your program • widget_name .configure( option =?, button = Tkinter.Button( master , text=?, command=?) option =?, …) • master is the window’s name • master is the window’s name – Changes as many options as you want all at once – Must be present when you create any widget • Demo: WindowWidgetOptions.py Winter 2011 CISC101 - Whittaker 23 Winter 2011 CISC101 - Whittaker 24 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Recommend


More recommend