Widgets
https://docs.python.org/3/library/tkinter.ttk.html#ttk-widgets
http://www.alan-g.me.uk/tutor/korean/tutgui.htm
Label
- Text Label
from tkinter import *
from tkinter import ttk
import antigravity
root = Tk()
label = ttk.Label(root, text = "Hello, Tkinter!")
label.pack()
label.config(text = "Howdy, Tkinter! It\'s been a while since we last met. Great to see you again!")
label.config(wraplength = 150)
label.config(justify = CENTER)
label.config(foreground = 'blue', background = 'yellow')
label.config(font = ('Courier', 18, 'bold'))
- Image Label
logo = PhotoImage(file = 'D:\Cloud\Dropbox\Workplace\pytest\Tkinter\Ex_Files_Python_Tkinter\Ex_Files_Python_Tkinter\Exercise Files\Ch03\python_logo.gif')
label.config(image = logo)
label.config(compound = 'text')
label.config(compound = 'center')
label.config(compound = 'left')
- GC에 의해 Image가 소멸되는 현상 방지
label.img = logo
logolabel.config(image = label.img)
Button
- Text & Image Button
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text = "Click Me")
button.pack()
logo = PhotoImage(file = 'D:\Cloud\Dropbox\Workplace\pytest\Tkinter\Ex_Files_Python_Tkinter\Ex_Files_Python_Tkinter\Exercise Files\Ch03\python_logo.gif')
button.config(image = logo, compound = LEFT)
small_logo = logo.subsample(5,5)
button.config(image = small_logo, compound = LEFT)
- Events
def callback():
print('Clicked!')
button.config(command = callback)
# 활성화 상태 확인
>>> button.invoke()
Clicked!
'None'
# 비활성화
>>> button.state(['disabled'])
('!disabled',)
>>> button.invoke()
''
>>> button.instate(['disabled'])
True
>>> button.state(['disabled'])
()
# 활성화
>>> button.state(['!disabled'])
('disabled',)
>>> button.invoke()
Clicked!
'None'
>>> button.instate(['disabled'])
False
Check Button & Radio Button
Text & Image Button
root = Tk() checkbutton = ttk.Checkbutton(root, text = 'SPAM?') checkbutton.pack()
Events StringVar()
>>> spam = StringVar()
>>> spam.set('SPAM!')
>>> spam.get()
'SPAM!'
>>> checkbutton.config(variable = spam, onvalue = 'SPAM Please!', offvalue = 'Boo SPAM')
>>> spam.get()
'SPAM Please!'
>>> spam.get()
'Boo SPAM'
Radio Button Example
>>> breakfast = StringVar() >>> ttk.Radiobutton(root, text = 'SPAM', variable = breakfast, value = 'SPAM').pack() >>> ttk.Radiobutton(root, text = 'Eggs', variable = breakfast, value = 'Eggs').pack() >>> ttk.Radiobutton(root, text = 'Sausage', variable = breakfast, value = 'Sausage').pack() >>> ttk.Radiobutton(root, text = 'SPAM', variable = breakfast, value = 'SPAM').pack() >>> >>> breakfast.get() 'SPAM'
Trick (Dynamic Button Label)
checkbutton.config(textvariable = breakfast)
Entry
>>> entry = ttk.Entry(root, width = 30)
>>> entry.pack()
>>> entry.get()
'sdsd'
>>> entry.delete(0, 1)
>>> entry.delete(0, END)
>>> entry.insert(0, 'Enter your password!')
>>> entry.config(show = '*')
>>> entry.get()
'Enter your password!'
>>> entry.state(['disabled'])
('!disabled',)
>>> entry.state(['!disabled'])
('disabled',)
>>> entry.state(['readonly'])
('!readonly',)
Combo Box & Spin Box
Combobox Example
>>> root = Tk() >>> month = StringVar() >>> combobox = ttk.Combobox(root, textvariable = month) >>> combobox.pack() >>> combobox.config(values = ('Jan', 'Feb', 'Mar')) >>> >>> month.get() 'Jan' >>> month.set('Mar') >>> month.set('Not a month')
Spinbox Example
>>> year = StringVar() >>> Spinbox(root, from_ = 1990, to = 2014, textvariable = year).pack()
Progress Bar
- Indeterminate Mode Examples
>>> root = Tk() >>> progressbar = ttk.Progressbar(root, orient = HORIZONTAL, length = 200) >>> progressbar.pack() >>> progressbar.config(mode = 'indeterminate') >>> progressbar.start() >>> progressbar.stop()
- Determinate Mode Examples
>>> progressbar.config(mode = 'determinate', maximum = 11.0, value = 4.2)
>>> progressbar.config(value = 8.0)
>>> progressbar.step()
>>> progressbar.step(5)
>>> value = DoubleVar()
>>> progressbar.config(variable = value)
>>> scale = ttk.Scale(root, orient = HORIZONTAL, length = 400, variable = value, from_ = 0.0, to = 11.0)
>>> scale.pack()
>>> scale.set(4.2)
>>> scale.get()
4.2