Programming

Python GUI 프로그래밍 tkinter 콤보박스 프로그래스바 메뉴바 메세지박스 프레임 그리드

신농해태 2021. 9. 14. 15:57
반응형

파이썬 GUI 프로그래밍 방법중 thkinter를 사용한 방법입니다. 

 

8. 콤보박스

import tkinter.ttk as ttk

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

values = [str(i)+"일" for i in range(1,32)]

combobox = ttk.Combobox(root, height=5, values=values)

combobox.pack()

combobox.set("결제일")

 

values = [str(i)+"일" for i in range(1,32)]

read_combobox = ttk.Combobox(root, height=10, values=values, state="readonly")

read_combobox.current(0)

read_combobox.pack()



def btncmd():

    print(combobox.get())

    print(read_combobox.get())

 

btn = Button(root, text="선택", command=btncmd)

btn.pack()

 

root.mainloop()

 

9. 프로그래스 바

import time

import tkinter.ttk as ttk

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

# progressbar = ttk.Progressbar(root, maximum=100, mode="indeterminate")

# progressbar = ttk.Progressbar(root, maximum=100, mode="determinate")

# progressbar.start(5)

# progressbar.pack()

 

# def btncmd():

#     progressbar.stop()

 

# btn = Button(root, text="중지", command=btncmd)

# btn.pack()

 

p_var2 = DoubleVar()

progressbar2 = ttk.Progressbar(root, maximum=100, length=150, variable=p_var2)

progressbar2.pack()

 

def btncmd():

    for i in range(1,101):

        time.sleep(0.01)

        p_var2.set(i)

        progressbar2.update()

        print(p_var2.get())

 

btn = Button(root, text="시작/중지", command=btncmd)

btn.pack()

 

root.mainloop()

 

 

10. 메뉴바

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

def create_new_file():

    print("새 파일 만들기")

 

menu = Menu(root)

 

menu_file = Menu(menu, tearoff = 0)

menu_file.add_command(label="New File", command=create_new_file)

menu_file.add_command(label="New Window")

menu_file.add_separator()

menu_file.add_command(label="Open File...")

menu_file.add_separator()

menu_file.add_command(label="Save All", state="disable")

menu_file.add_separator()

menu_file.add_command(label="Exit", command=root.quit)

menu.add_cascade(label="File", menu=menu_file)

 

menu.add_cascade(label="Edit")

 

menu_lang = Menu(menu, tearoff=0)

menu_lang.add_radiobutton(label="Python")

menu_lang.add_radiobutton(label="Java")

menu_lang.add_radiobutton(label="C++")

menu.add_cascade(label="Language", menu=menu_lang)

 

menu_view = Menu(menu, tearoff=0)

menu_view.add_checkbutton(label="Show Minimap")

menu.add_cascade(label="View", menu=menu_view)

 

root.config(menu=menu)

 

root.mainloop()

 

 

11. 메세지박스

import tkinter.messagebox as msgbox

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

def info():

    msgbox.showinfo("알림","정상적으로 예약됨")

 

def warn():

    msgbox.showwarning("중요","예약 매진")

 

def error():

    msgbox.showerror("오류","일정 오류")

 

def okcancel():

    msgbox.askokcancel("대기","예약 대기. 예약 확인?")

 

def retrycancel():

    msgbox.askretrycancel("취소확인","예약 오류. 다시 예약?")

 

def yesno():

    msgbox.askyesno("예 아니오","VIP석. 예약?")

 

def yesnocancel():

    response = msgbox.askyesnocancel(title=None, message="예매진쟁중. 종료?")

    print("응답:", response)

    if response == 1: 

        print("예")

    elif response == 0:

        print("아니오")

    else:

        print("취소")



Button(root, command=info, text="알림").pack()

Button(root, command=warn, text="중요").pack()

Button(root, command=error, text="오류").pack()

Button(root, command=okcancel, text="취소").pack()

Button(root, command=retrycancel, text="다시예약").pack()

Button(root, command=yesno, text="예 아니오").pack()

Button(root, command=yesnocancel, text="예 아니오 취소").pack()

 

root.mainloop()

 

12. 프레임

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

Label(root, text="메뉴").pack(side="top")

 

Button(root, text="주문하기").pack(side="bottom")

 

frame_food = Frame(root, relief="solid", bd=1)

frame_food.pack(side="left", fill="both", expand=True)

 

Button(frame_food, text="떡볶이").pack()

Button(frame_food, text="오뎅").pack()

Button(frame_food, text="순대").pack()

 

frame_drink = LabelFrame(root, text="음료")

frame_drink.pack(side="right", fill="both", expand=True)

Button(frame_drink, text="우유").pack()

Button(frame_drink, text="콜라").pack()

 

root.mainloop()

 

 

13. 스크롤바

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

frame = Frame(root)

frame.pack()

 

scrollbar = Scrollbar(frame)

scrollbar.pack(side="right", fill = "y")

 

listbox = Listbox(frame, selectmode="extended", height=10, yscrollcommand = scrollbar.set)

for i in range(1,32):

    listbox.insert(END, str(i) + "일")

listbox.pack(side="left")

 

scrollbar.config(command=listbox.yview)

 

root.mainloop()

 

 

14. 그리드

그리드를 이용하여 계산기 모양을 만들어 봅니다. 

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

btn_f16 = Button(root, text="F16")

btn_f17 = Button(root, text="F17")

btn_f18 = Button(root, text="F18")

btn_f19 = Button(root, text="F19")

 

btn_f16.grid(row=0, column=0)

btn_f17.grid(row=0, column=1)

btn_f18.grid(row=0, column=2)

btn_f19.grid(row=0, column=3)

 

btn_clear = Button(root, text="clear")

btn_equal = Button(root, text="=")

btn_div = Button(root, text="/")

btn_mul = Button(root, text="*")

 

btn_clear.grid(row=1, column=0)

btn_equal.grid(row=1, column=1)

btn_div.grid(row=1, column=2)

btn_mul.grid(row=1, column=3)

 

btn_7 = Button(root, text="7")

btn_8 = Button(root, text="8")

btn_9 = Button(root, text="9")

btn_10 = Button(root, text="-")

 

btn_7.grid(row=2, column=0)

btn_8.grid(row=2, column=1)

btn_9.grid(row=2, column=2)

btn_10.grid(row=2, column=3)

 

btn_4 = Button(root, text="4")

btn_5 = Button(root, text="5")

btn_6 = Button(root, text="6")

btn_add = Button(root, text="+")

 

btn_4.grid(row=3, column=0)

btn_5.grid(row=3, column=1)

btn_6.grid(row=3, column=2)

btn_add.grid(row=3, column=3)

 

btn_1 = Button(root, text="1")

btn_2 = Button(root, text="2")

btn_3 = Button(root, text="3")

btn_enter = Button(root, text="enter")

 

btn_1.grid(row=4, column=0)

btn_2.grid(row=4, column=1)

btn_3.grid(row=4, column=2)

btn_enter.grid(row=4, column=3, rowspan=2)

 

btn_0 = Button(root, text="0")

btn_point = Button(root, text=".")

 

btn_0.grid(row=5, column=0, columnspan=2)

btn_point.grid(row=5, column=2)

 

root.mainloop()

 

 

크기와 간격 조정하기

 

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

btn_f16 = Button(root, text="F16", padx=10, pady=10)

btn_f17 = Button(root, text="F17", padx=10, pady=10)

btn_f18 = Button(root, text="F18", padx=10, pady=10)

btn_f19 = Button(root, text="F19", padx=10, pady=10)

 

btn_f16.grid(row=0, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_f17.grid(row=0, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_f18.grid(row=0, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_f19.grid(row=0, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_clear = Button(root, text="clear", padx=10, pady=10)

btn_equal = Button(root, text="=", padx=10, pady=10)

btn_div = Button(root, text="/", padx=10, pady=10)

btn_mul = Button(root, text="*", padx=10, pady=10)

 

btn_clear.grid(row=1, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_equal.grid(row=1, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_div.grid(row=1, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_mul.grid(row=1, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_7 = Button(root, text="7", padx=10, pady=10)

btn_8 = Button(root, text="8", padx=10, pady=10)

btn_9 = Button(root, text="9", padx=10, pady=10)

btn_sub = Button(root, text="-", padx=10, pady=10)

 

btn_7.grid(row=2, column=0, sticky=W)

btn_8.grid(row=2, column=1, sticky=W)

btn_9.grid(row=2, column=2, sticky=W)

btn_sub.grid(row=2, column=3, sticky=W)

 

btn_4 = Button(root, text="4", padx=10, pady=10)

btn_5 = Button(root, text="5", padx=10, pady=10)

btn_6 = Button(root, text="6", padx=10, pady=10)

btn_add = Button(root, text="+", padx=10, pady=10)

 

btn_4.grid(row=3, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_5.grid(row=3, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_6.grid(row=3, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_add.grid(row=3, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_1 = Button(root, text="1", padx=10, pady=10)

btn_2 = Button(root, text="2", padx=10, pady=10)

btn_3 = Button(root, text="3", padx=10, pady=10)

btn_enter = Button(root, text="enter", padx=10, pady=10)

 

btn_1.grid(row=4, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_2.grid(row=4, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_3.grid(row=4, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_enter.grid(row=4, column=3, rowspan=2, sticky=N+E+W+S, padx=3, pady=3)

 

btn_0 = Button(root, text="0", padx=10, pady=10)

btn_point = Button(root, text=".", padx=10, pady=10)

 

btn_0.grid(row=5, column=0, columnspan=2, sticky=N+E+W+S, padx=3, pady=3)

btn_point.grid(row=5, column=2, sticky=N+E+W+S, padx=3, pady=3)

 

root.mainloop()

 

 

다른 방법으로 크기와 간격을 조정해보기

 

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

btn_f16 = Button(root, text="F16", width=5, height=2)

btn_f17 = Button(root, text="F17", width=5, height=2)

btn_f18 = Button(root, text="F18", width=5, height=2)

btn_f19 = Button(root, text="F19", width=5, height=2)

 

btn_f16.grid(row=0, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_f17.grid(row=0, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_f18.grid(row=0, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_f19.grid(row=0, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_clear = Button(root, text="clear", width=5, height=2)

btn_equal = Button(root, text="=", width=5, height=2)

btn_div = Button(root, text="/", width=5, height=2)

btn_mul = Button(root, text="*", width=5, height=2)

 

btn_clear.grid(row=1, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_equal.grid(row=1, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_div.grid(row=1, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_mul.grid(row=1, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_7 = Button(root, text="7", width=5, height=2)

btn_8 = Button(root, text="8", width=5, height=2)

btn_9 = Button(root, text="9", width=5, height=2)

btn_sub = Button(root, text="-", width=5, height=2)

 

btn_7.grid(row=2, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_8.grid(row=2, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_9.grid(row=2, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_sub.grid(row=2, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_4 = Button(root, text="4", width=5, height=2)

btn_5 = Button(root, text="5", width=5, height=2)

btn_6 = Button(root, text="6", width=5, height=2)

btn_add = Button(root, text="+", width=5, height=2)

 

btn_4.grid(row=3, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_5.grid(row=3, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_6.grid(row=3, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_add.grid(row=3, column=3, sticky=N+E+W+S, padx=3, pady=3)

 

btn_1 = Button(root, text="1", width=5, height=2)

btn_2 = Button(root, text="2", width=5, height=2)

btn_3 = Button(root, text="3", width=5, height=2)

btn_enter = Button(root, text="enter", width=5, height=2)

 

btn_1.grid(row=4, column=0, sticky=N+E+W+S, padx=3, pady=3)

btn_2.grid(row=4, column=1, sticky=N+E+W+S, padx=3, pady=3)

btn_3.grid(row=4, column=2, sticky=N+E+W+S, padx=3, pady=3)

btn_enter.grid(row=4, column=3, rowspan=2, sticky=N+E+W+S, padx=3, pady=3)

 

btn_0 = Button(root, text="0", width=5, height=2)

btn_point = Button(root, text=".", width=5, height=2)

 

btn_0.grid(row=5, column=0, columnspan=2, sticky=N+E+W+S, padx=3, pady=3)

btn_point.grid(row=5, column=2, sticky=N+E+W+S, padx=3, pady=3)

 

root.mainloop()

 

Python GUI 프로그래밍 tkinter 콤보박스 프로그래스바 메뉴바 메세지박스 프레임 그리드

728x90
LIST