[참고]

 

1. 소스

# -*- coding: utf-8 -*-

import tkinter as tk 
from tkinter import ttk
from tkinter import scrolledtext

win = tk.Tk()

win.title("Python GUI")

#Label 만들기
a_label = ttk.Label(win, text = "A Label")
a_label.grid(column = 0, row = 0)

#버튼 동작함수
def click_me():
    action.configure(text = 'Hello ' + name.get() + ' '+ number_chosen.get())

#Label 만들기
ttk.Label(win, text = "Enter a name:").grid(column = 0, row = 0)

# 빈 텍스트박스 위젯 만들기
name = tk.StringVar()
name_entered = ttk.Entry(win, width = 12, textvariable = name)
name_entered.grid(column = 0, row = 1)

#버튼 만들기
action = ttk.Button(win, text = "Click Me!", command = click_me)
action.grid(column = 2, row = 1)

#콤보박스 만들기
ttk.Label(win, text = "Choose a number:").grid(column = 1, row = 0)
number = tk.StringVar()
number_chosen = ttk.Combobox(win, width = 12, textvariable = number, state = 'readonly')
number_chosen['values'] = (1,2,4,42,100)
number_chosen.grid(column = 1, row = 1)
number_chosen.current(0)

#체크버튼 만들기
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text='Disabled', variable = chVarDis, state = 'disabled')
check1.select()
check1.grid(column=0, row=4, sticky = tk.W)

chVarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text = 'UnChecked', variable = chVarUn)
check2.deselect()
check2.grid(column = 1, row = 4)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text= 'Enabled', variable = chVarEn)
check3.deselect()
check3.grid(column = 2, row = 4, sticky = tk.W)

#GUI Callback 함수
def checkCallback(*ignoredArgs):

    if chVarUn.get():
        check3.configure(state = 'disabled')
    else:
        check3.configure(state = 'normal')
    if chVarEn.get():
        check2.configure(state = 'disabled')
    else:
        check2.configure(state = 'normal')

#2개 체크버튼 상태 추적
chVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())

#스크롤 문자 사용
scrol_w = 30
scrol_h = 3
scr = scrolledtext.ScrolledText(win, width = scrol_w, height = scrol_h, wrap = tk.WORD)
scr.grid(column = 0, row = 5, columnspan = 3)

#라디오 버튼을 리스트로 변경
colors = ["Blue", "Gold", "Red"]

def radCall():
    radSel = radVar.get()
    if radSel == 0:
        win.configure(background=colors[0])
    elif radSel == 1:
        win.configure(background=colors[1])
    elif radSel == 2:
        win.configure(background=colors[2])

# 한 개의 변수를 사용해 3개의 라디오 버튼 생성
radVar = tk.IntVar()

#radVar에 존재하지 않는 인덱스 값 선택
radVar.set(99)

#한 루프 이내에 3개의 라디오 버튼 생성
for col in range(3):
    curRad = tk.Radiobutton(win, text = colors[col], variable = radVar, value = col, command = radCall)
    curRad.grid(column = col, row = 6, sticky = tk.W)

#라벨을 유지하기 위한 컨테이너 생성
buttons_frame = ttk.LabelFrame(win, text='Labels in a Frame ')
buttons_frame.grid(column = 1, row = 7)

#컨테이너 요소로 라벨 놓기
ttk.Label(buttons_frame, text = 'Label1').grid(column = 0, row = 0, sticky = tk.W)
ttk.Label(buttons_frame, text = 'Label2').grid(column = 1, row = 0, sticky = tk.W)
ttk.Label(buttons_frame, text = 'Label3').grid(column = 2, row = 0, sticky = tk.W)

#name Entry로 커서이동
name_entered.focus()

#GUI 시작
win.mainloop()

2. 결과

 

+ Recent posts