상세 컨텐츠

본문 제목

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

Programming

by 신농해태 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(rootheight=5values=values)

combobox.pack()

combobox.set("결제일")

 

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

read_combobox = ttk.Combobox(rootheight=10values=valuesstate="readonly")

read_combobox.current(0)

read_combobox.pack()



def btncmd():

    print(combobox.get())

    print(read_combobox.get())

 

btn = Button(roottext="선택"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(rootmaximum=100length=150variable=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(roottext="시작/중지"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(menutearoff = 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(menutearoff=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(menutearoff=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=Nonemessage="예매진쟁중. 종료?")

    print("응답:"response)

    if response == 1

        print("예")

    elif response == 0:

        print("아니오")

    else:

        print("취소")



Button(rootcommand=infotext="알림").pack()

Button(rootcommand=warntext="중요").pack()

Button(rootcommand=errortext="오류").pack()

Button(rootcommand=okcanceltext="취소").pack()

Button(rootcommand=retrycanceltext="다시예약").pack()

Button(rootcommand=yesnotext="예 아니오").pack()

Button(rootcommand=yesnocanceltext="예 아니오 취소").pack()

 

root.mainloop()

 

12. 프레임

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

Label(roottext="메뉴").pack(side="top")

 

Button(roottext="주문하기").pack(side="bottom")

 

frame_food = Frame(rootrelief="solid"bd=1)

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

 

Button(frame_foodtext="떡볶이").pack()

Button(frame_foodtext="오뎅").pack()

Button(frame_foodtext="순대").pack()

 

frame_drink = LabelFrame(roottext="음료")

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

Button(frame_drinktext="우유").pack()

Button(frame_drinktext="콜라").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(frameselectmode="extended"height=10yscrollcommand = scrollbar.set)

for i in range(1,32):

    listbox.insert(ENDstr(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(roottext="F16")

btn_f17 = Button(roottext="F17")

btn_f18 = Button(roottext="F18")

btn_f19 = Button(roottext="F19")

 

btn_f16.grid(row=0column=0)

btn_f17.grid(row=0column=1)

btn_f18.grid(row=0column=2)

btn_f19.grid(row=0column=3)

 

btn_clear = Button(roottext="clear")

btn_equal = Button(roottext="=")

btn_div = Button(roottext="/")

btn_mul = Button(roottext="*")

 

btn_clear.grid(row=1column=0)

btn_equal.grid(row=1column=1)

btn_div.grid(row=1column=2)

btn_mul.grid(row=1column=3)

 

btn_7 = Button(roottext="7")

btn_8 = Button(roottext="8")

btn_9 = Button(roottext="9")

btn_10 = Button(roottext="-")

 

btn_7.grid(row=2column=0)

btn_8.grid(row=2column=1)

btn_9.grid(row=2column=2)

btn_10.grid(row=2column=3)

 

btn_4 = Button(roottext="4")

btn_5 = Button(roottext="5")

btn_6 = Button(roottext="6")

btn_add = Button(roottext="+")

 

btn_4.grid(row=3column=0)

btn_5.grid(row=3column=1)

btn_6.grid(row=3column=2)

btn_add.grid(row=3column=3)

 

btn_1 = Button(roottext="1")

btn_2 = Button(roottext="2")

btn_3 = Button(roottext="3")

btn_enter = Button(roottext="enter")

 

btn_1.grid(row=4column=0)

btn_2.grid(row=4column=1)

btn_3.grid(row=4column=2)

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

 

btn_0 = Button(roottext="0")

btn_point = Button(roottext=".")

 

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

btn_point.grid(row=5column=2)

 

root.mainloop()

 

 

크기와 간격 조정하기

 

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

btn_f16 = Button(roottext="F16"padx=10pady=10)

btn_f17 = Button(roottext="F17"padx=10pady=10)

btn_f18 = Button(roottext="F18"padx=10pady=10)

btn_f19 = Button(roottext="F19"padx=10pady=10)

 

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

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

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

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

 

btn_clear = Button(roottext="clear"padx=10pady=10)

btn_equal = Button(roottext="="padx=10pady=10)

btn_div = Button(roottext="/"padx=10pady=10)

btn_mul = Button(roottext="*"padx=10pady=10)

 

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

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

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

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

 

btn_7 = Button(roottext="7"padx=10pady=10)

btn_8 = Button(roottext="8"padx=10pady=10)

btn_9 = Button(roottext="9"padx=10pady=10)

btn_sub = Button(roottext="-"padx=10pady=10)

 

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

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

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

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

 

btn_4 = Button(roottext="4"padx=10pady=10)

btn_5 = Button(roottext="5"padx=10pady=10)

btn_6 = Button(roottext="6"padx=10pady=10)

btn_add = Button(roottext="+"padx=10pady=10)

 

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

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

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

btn_add.grid(row=3column=3sticky=N+E+W+Spadx=3pady=3)

 

btn_1 = Button(roottext="1"padx=10pady=10)

btn_2 = Button(roottext="2"padx=10pady=10)

btn_3 = Button(roottext="3"padx=10pady=10)

btn_enter = Button(roottext="enter"padx=10pady=10)

 

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

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

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

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

 

btn_0 = Button(roottext="0"padx=10pady=10)

btn_point = Button(roottext="."padx=10pady=10)

 

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

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

 

root.mainloop()

 

 

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

 

from tkinter import *

 

root = Tk()

root.title("Haetae GUI")

root.geometry("640x480")

 

btn_f16 = Button(roottext="F16"width=5height=2)

btn_f17 = Button(roottext="F17"width=5height=2)

btn_f18 = Button(roottext="F18"width=5height=2)

btn_f19 = Button(roottext="F19"width=5height=2)

 

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

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

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

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

 

btn_clear = Button(roottext="clear"width=5height=2)

btn_equal = Button(roottext="="width=5height=2)

btn_div = Button(roottext="/"width=5height=2)

btn_mul = Button(roottext="*"width=5height=2)

 

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

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

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

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

 

btn_7 = Button(roottext="7"width=5height=2)

btn_8 = Button(roottext="8"width=5height=2)

btn_9 = Button(roottext="9"width=5height=2)

btn_sub = Button(roottext="-"width=5height=2)

 

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

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

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

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

 

btn_4 = Button(roottext="4"width=5height=2)

btn_5 = Button(roottext="5"width=5height=2)

btn_6 = Button(roottext="6"width=5height=2)

btn_add = Button(roottext="+"width=5height=2)

 

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

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

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

btn_add.grid(row=3column=3sticky=N+E+W+Spadx=3pady=3)

 

btn_1 = Button(roottext="1"width=5height=2)

btn_2 = Button(roottext="2"width=5height=2)

btn_3 = Button(roottext="3"width=5height=2)

btn_enter = Button(roottext="enter"width=5height=2)

 

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

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

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

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

 

btn_0 = Button(roottext="0"width=5height=2)

btn_point = Button(roottext="."width=5height=2)

 

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

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

 

root.mainloop()

 

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

728x90
LIST

관련글 더보기

댓글 영역