python์ tkinter ๋ชจ๋์ ์ฌ์ฉํด์ ๋จ์ ๋ณํ๊ธฐ๋ฅผ ๋ง๋ค์ด ๋ณด์.
์์ง tkinter ๋ชจ๋์ ์ฒ์ ๊ณต๋ถํ๋ ์ค์ด๊ธฐ ๋๋ฌธ์, ๊ฐ๋จํ๊ฒ ๊ตฌํํด๋ณด์๋ค.
1. miles to km, km to miles 2๊ฐ์ง ์ต์ ๊ตฌํ
2. ๋ฒํผ์ ๋๋ฅผ ์ ๊ณ์ฐ์ด ์๋ฃ๋์ด ์๋ฃ ๊ฐ์ด ์ถ๋ ฅ
์ฝ๋
from tkinter import *
window = Tk()
FONT = ("Arial", 20)
from_unit = "miles"
to_unit = "km"
window.title("Unit Converter")
window.minsize(width="250", height="100")
window.config(padx=50, pady=25)
# Label
from_label = Label(text=f"{from_unit}", font=FONT)
from_label.grid(column=2, row=2, padx=5, pady=5)
to_label = Label(text=f"{to_unit}", font=FONT)
to_label.grid(column=2, row=3, padx=5, pady=5)
result_label = Label(text="0", font=FONT)
result_label.grid(column=1, row=3, padx=5, pady=5)
equal_to_label = Label(text="is equal to", font=FONT)
equal_to_label.grid(column=0, row=3, padx=5, pady=5)
empty_label = Label(text="", font=FONT)
empty_label.grid(column=1, row=1)
arrow_label = Label(text="โก๏ธโก๏ธโก๏ธ", font=FONT)
arrow_label.grid(column=0, row=2)
# Entry
num_input = Entry(width=10)
num_input.grid(column=1, row=2, padx=5, pady=5)
# Radio Button
def label_update():
from_label.config(text=from_unit)
to_label.config(text=to_unit)
def change_mode(mode):
global from_unit, to_unit
if mode == "miles":
from_unit = "miles"
to_unit = "km"
else:
from_unit = "km"
to_unit = "miles"
label_update()
mode_select = StringVar()
miles_button = Radiobutton(text="miles -> km", value="miles", variable=mode_select, command=lambda: change_mode("miles"))
miles_button.select()
km_button = Radiobutton(text="km -> miles", value="km", variable=mode_select, command=lambda: change_mode("km"))
miles_button.grid(column=0, row=0)
km_button.grid(column=2, row=0)
# Button
def calculate(unit, value):
if value:
if unit == "miles":
result = miles_to_km(value)
elif unit == "km":
result = km_to_miles(value)
result_label.config(text=round(result, 3))
def miles_to_km(value):
return float(value) * 1.609
def km_to_miles(value):
return float(value) * 0.621
calculate_button = Button(text="CONVERT", command=lambda: calculate(unit=from_unit, value=num_input.get()))
calculate_button.grid(column=1, row=4)
window.mainloop()
์คํ ๊ฒฐ๊ณผ
'python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[baekjoon] 10789๋ฒ ์ธ๋ก์ฝ๊ธฐ Python (1) | 2023.10.14 |
---|---|
[programmers] ๋ฌธ์์ด ์ฌ๋ฌ๋ฒ ๋ค์ง๊ธฐ (python) (8) | 2023.10.11 |
[python] *args ๊ณผ **kwargs (6) | 2023.10.10 |
[python] list comprehension ๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ (0) | 2023.10.09 |
[python] code-runner extension ํ์ผ ์คํ ์ ํฐ๋ฏธ๋์ ์ด๊ธฐํ, file path ์ถ๋ ฅ ์๋๊ฒ ๋ณ๊ฒฝ (0) | 2023.10.08 |