tkinter 콤보박스 간단사용예

 tkinter 의 간단한 콤보박스 사용예입니다. 

콤보박스에서 컬러를 선택하면 그 아래 Label 의 배경색이 변합니다. 

그 아래 Progressbar 는 그냥 심심해서(?) 넣어 본것 입니다. indeterminate 모드로 넣은 것인데 indeterminate 모드는 정확한 진행정도를 나타내는게 아니라 그냥 진행중이라는걸 표시하는 것입니다. 진행바가 그냥 좌우로 움직이기만 합니다. 

from tkinter import *
from tkinter.ttk import *
from tkinter.messagebox import *
 
class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack(side="top")
        self.create_widgets()
 
    def create_widgets(self):
        self.cb = Combobox(width=10,value=['white','blue','yellow','black','red'])
        self.cb.current(0)
        self.cb.bind("<<ComboboxSelected>>", self.change_color)
        self.cb.pack()
        
        self.style = Style()
        self.style.configure("BW.TLabel", foreground="black", background="white")
         
        self.l1 = Label(text = "Color Label", style = "BW.TLabel")
        self.l1.pack()
         
        p = Progressbar(mode='indeterminate')
        p.pack()
        p.start(15)
         
    def change_color(self,event):
        color = self.cb.get()
        self.style.configure("BW.TLabel", foreground="black", background=color)
 
root = Tk()
root.title("tk 테스트")
root.geometry("300x200+500+500")
app = Application(master=root)
app.mainloop()


기본적인 틀은 이전 예제와 같습니다. 

콤보박스 생성시에 컬러 이름을 넣고 <<ComboSelected>> 이벤트가 발생하면(콤보박스에서 특정색을 선택하면) change_color 함수를 실행해서 Label 의 Style 에서 background color 를 변경해 줍니다.

ProgressBar 는 간단히 indeterminate 모드로 생성해서 Start 해 줍니다. Start 시에 인자는 다음 이벤트가 발생하는 시간으로 시간이 작으면 더 빨리 움직입니다.

실행화면 입니다. 간단한 사용예인 만큼 디자인은 신경쓰지 않는 걸로....^^;

댓글