If you have the textbook read Chapter 13, GUI Programming.
def count_down(num): print(num) if num > 1: count_down(num - 1)
10 9 8 7 6 5 4 3 2 1
n!
n! = (n-1)! * n
1! = 1
def factorial(num): if num == 1: return 1 else: return factorial(num -1) * num
>>> print("5!:",factorial(5)) 5!: 120
1 1
1 1 2 3 5 8 13 21 34 55
F(n) = F(n - 1) + F(n - 2)
F(1) = 1 F(2) = 1
def fibonacci(num): if num == 1 or num == 2: return 1 else: return fibonacci(num - 1) + fibonacci(num - 2)
for
loop
for n in range(1, 11): print(fibonacci(n), end=" ")
1 1 2 3 5 8 13 21 34 55
set a counter to 0
get a list of all the entries in the current directory
for each entry
if the entry is a directory
increase the count by 1
run the function on this new directory
and increase the count by what it returns
return the count
def dir_count(path): count = 0 entries = os.listdir(path) for entry in entries: entry_path = path + "/" + entry if os.path.isdir(entry_path): count += 1 count += dir_count(entry_path) return count
set a factorial variable to 1
set a count variable to 1
while count is less than the number whose factorial we are computing
increment count
set factorial to count times the current value of factorial
return the factorial value
def factorial(number): value = 1 count = 1 while count < number: count += 1 value *= count return value
if the number is 1
return 1
else:
set the first number variable to 0
set the second number variable to 1
set count to 1
while count is less than the number
increment count by 1
set value to first number plus the second number
set first to second
set second to value
return value
def fibonacci(number): if number == 1: return 1 else: first = 0 second = 1 count = 1 while count < number: count += 1 value = first + second first = second second = value return value
def fibonacci(num): if num == 1 or num == 2: return 1 else: return fibonacci(num - 1) + fibonacci(num - 2)
#! /usr/bin/python3 import tkinter class EmptyWindow: def __init__(self): # Create the main window widget. self.main_window = tkinter.Tk() # Enter the tkinter main loop. tkinter.mainloop() root = EmptyWindow()
#! /usr/bin/python3
import tkinter
class EmptyWindow:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Empty Window")
tkinter.mainloop()
root = EmptyWindow()
WIDTHxHEIGHT
#! /usr/bin/python3
import tkinter
class EmptyWindow:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Empty Window")
self.main_window.geometry("400x200")
tkinter.mainloop()
root = EmptyWindow()
WIDTHxHEIGHT+X_COORDINATE+Y_COORDINATE
Widget | Description |
---|---|
Button | A button that can cause an action to occur when it is clicked |
Canvas | A rectangular area that can be used to display graphics |
Checkbutton | A button that may be in either the "on" or "off" position |
Entry | An area in which the user may type a single line of input from the keyboard |
Frame | A container that can hold other widgets |
Label | An area that displays one line of text or an image |
Listbox | A list from which the user may select an item |
Menu | A list of menu choices that are displayed when the user clicks a Menu widget |
Menubutton | The part of a drop-down menu that stays on the screen all the time |
Message | Displays multiple lines of text |
Radiobutton | A widget that can be either selected or deselected. Radiobutton widgets usually appear in groups and allow the user to select one of several options |
Scale | A widget that allows the user to select a value by moving a slider along a track |
Scrollbar | Can be used with some other types of widgets to provide scrolling ability |
Text | A widget that allows the user to enter multiple lines of text input |
Toplevel | A container, like a Frame, but displayed in its own window |
import tkinter class HelloWindow: def __init__(self): self.main_window = tkinter.Tk() self.main_window.title("Hello") self.main_window.geometry("400x200") self.label = tkinter.Label(self.main_window, text="Hello, world!") self.label.pack() tkinter.mainloop() root = HelloWindow()
self.label = tkinter.Label(self.main_window, text="Hello, world!", \ font="times 24 bold")
import tkinter
self.main_window = tkinter.Tk() ... self.label = tkinter.Label(self.main_window, text="Hello, world!", font="times 24 bold") ... tkinter.mainloop()
from tkinter import *
self.main_window = Tk() ... self.label = Label(self.main_window, text="Hello, world!", \ font="times 24 bold") ... mainloop()
self.label = Label(self.main_window, text="Hello, world!", \
font="times 24 bold", fg="blue")
from tkinter import * class ColorWindow: def __init__(self): self.main_window = Tk() self.main_window.title("Colors") self.main_window.geometry("400x200") self.red_label = Label(self.main_window, text="Red text", fg="red") self.red_label.pack() self.orange_label = Label(self.main_window, text="Orange text", fg="orange") self.orange_label.pack() self.yellow_label = Label(self.main_window, text="Yellow text", fg="yellow") self.yellow_label.pack() self.green_label = Label(self.main_window, text="Green text", fg="green") self.green_label.pack() self.blue_label = Label(self.main_window, text="Blue text", fg="blue") self.green_label.pack() self.violet_label = Label(self.main_window, text="Violet text", fg="violet") self.violet_label.pack() self.cyan_label = Label(self.main_window, text="Cyan text", fg="cyan") self.cyan_label.pack() self.magenta_label = Label(self.main_window, text="Magenta text", fg="magenta") self.magenta_label.pack() self.brown_label = Label(self.main_window, text="Brown text", fg="brown") self.brown_label.pack() mainloop() root = ColorWindow()