Day 027

AWESOMee·2022년 2월 21일
0

Udemy Python Bootcamp

목록 보기
27/64
post-thumbnail

Udemy Python Bootcamp Day 027

Tkinter
Graphical
User
Interface

GUIs were a huge deal in the nineties bacause previously, in order to interact with a computer, we had to use a command line operationg system such as MS-DOS.

GUIs are graphical, and accompanied with a mouse, they allow a user with very little training in computers to be able to just point and click on the thing that they want to do on the screen.

Labels with tkinter

import tkinter

window = tkinter.Tk()
window.title("My First GUI Program")
window.minsize(width=500, height=300)

# Label

my_label = tkinter.Label(text="I Am a Label", font=("Arial", 24, "bold"))
my_label.pack()

my_label["text"] = "New Text"  # or my_label.config(text="New Text")

window.mainloop()

.mainloop() is th thing that will keep the window on screen.

The Packer
The packer is one of Tk’s geometry-management mechanisms.
It's just a simple way to lay out the components that we're building.

Arguments with Default Values

def my_function(a=1, b=2, c=3):
	#Do this with a
    #Then do this with b
    #Finally so this with c
    
my_function()  # a=1, b=2, c=3
my_function(b=5)  # a=1, b=5, c=3

Unlimited Arguments

def add(*args):
	for n in args:
    	print(n)

asterix * : What that tells Python is this function add can accept any number of arguments. And once inside the function, we can actually loop through all of the arguments which is going to be in the form of a tuple and we can do whatever it is we want with each of those arguments.

Args

def add(*args):
    print(args[1])
    
    sum = 0
    for n in args:
        sum += n
    return sum

print(add(1, 2, 3, 4, 5, 6, 7, 8, 9))

#output
2
45

Kwargs

def calculate(n, **kwargs):
    print(kwargs)
    # for key, value in kwargs.items():
    #     print(key)
    #     print(value)
    n += kwargs["add"]
    n *= kwargs["multiply"]
    print(n)

calculate(2, add=3, multiply=5)

#output
{'add': 3, 'multiply': 5}
25

class Car:

    def __init__(self, **kw):
        self.make = kw["make"]
        self.model = kw["model"]

my_car = Car(make="Nissan", model="GT-R")
print(my_car.make)

#output
Nissan

class Car:

    def __init__(self, **kw):
        self.make = kw["make"]
        self.model = kw["model"]

my_car = Car(make="Nissan")
print(my_car.model)

#output
KeyError: 'model'

.get()

class Car:

    def __init__(self, **kw):
        self.make = kw["make"]
        self.model = kw.get("model")

my_car = Car(make="Nissan")
print(my_car.model)

#output
none

Buttons and Entry with tkinter

# Button


def button_clicked():
    new_text = input.get()
    my_label.config(text=new_text)

button = Button(text="Click Me", command=button_clicked)
button.pack()

# Entry

input = Entry(width=10)
input.pack()
print(input.get())

tkinter widget

#Text
text = Text(height=5, width=30)
#Puts cursor in textbox.
text.focus()
#Adds some text to begin with.
text.insert(END, "Example of multi-line text entry.")
#Get's current value in textbox at line 1, character 0
print(text.get("1.0", END))
text.pack()


#Spinbox
def spinbox_used():
    #gets the current value in spinbox.
    print(spinbox.get())
spinbox = Spinbox(from_=0, to=10, width=5, command=spinbox_used)
spinbox.pack()


#Scale
#Called with current scale value.
def scale_used(value):
    print(value)
scale = Scale(from_=0, to=100, command=scale_used)
scale.pack()


#Checkbutton
def checkbutton_used():
    #Prints 1 if On button checked, otherwise 0.
    print(checked_state.get())
#variable to hold on to checked state, 0 is off, 1 is on.
checked_state = IntVar()
checkbutton = Checkbutton(text="Is On?", variable=checked_state, command=checkbutton_used)
checked_state.get()
checkbutton.pack()


#Radiobutton
def radio_used():
    print(radio_state.get())
#Variable to hold on to which radio button value is checked.
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Option1", value=1, variable=radio_state, command=radio_used)
radiobutton2 = Radiobutton(text="Option2", value=2, variable=radio_state, command=radio_used)
radiobutton1.pack()
radiobutton2.pack()


#Listbox
def listbox_used(event):
    # Gets current selection from listbox
    print(listbox.get(listbox.curselection()))

listbox = Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits:
    listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
listbox.pack()

Tkinter Layout : Pack, Place, Grid

We can't mix up grid and pack in the came program.

from tkinter import *

window = Tk()
window.title("My First GUI Program")
window.minsize(width=500, height=300)
window.config(padx=100, pady=200)

# Label
my_label = Label(text="I Am a Label", font=("Arial", 24, "bold"))
my_label.config(text="New Text")
my_label.grid(column=0, row=0)
my_label.config(padx=50, pady=50)

button = Button(text="Click Me")
button.grid(column=1, row=1)

new_button = Button(text="New Button")
new_button.grid(column=2, row=0)

# Entry
input = Entry(width=10)
print(input.get())
input.grid(column=3, row=2)

window.mainloop()

Mile to Km Converter

from tkinter import *


def miles_to_km():
    miles = float(miles_input.get())
    km = round(miles * 1.609)
    kilometer_result_label.config(text=f"{km}")
    
    
window = Tk()
window.title("Mile to Kilometer Converter")
window.config(padx=20, pady=20)

miles_input = Entry(width=7)
miles_input.grid(column=1, row=0)

miles_label = Label(text="Miles")
miles_label.grid(column=2, row=0)

is_equal_label = Label(text="is equal to")
is_equal_label.grid(column=0, row=1)

kilometer_result_label = Label(text="0")
kilometer_result_label.grid(column=1, row=1)

kilometer_label = Label(text="Km")
kilometer_label.grid(column=2, row=1)

calculate_button = Button(text="Calculate", command=miles_to_km)
calculate_button.grid(column=1, row=2)


window.mainloop()

i made it!!!!!!!

profile
개발을 배우는 듯 하면서도

0개의 댓글