Python tkinter

Ref: https://docs.python.org/3/library/tkinter.html

Widgets

  • tkinter.Button
  • tkinter.CheckButton
  • tkinter.RadioButton
  • tkinter.MenuButton
  • tkinter.Menu
  • tkinter.Scale
  • tkinter.Listbox
  • tkinter.SpinBox
  • tkinter.Text: ํ…์ŠคํŠธ ์—ฌ๋Ÿฌ ์ค„ ์ž…/์ถœ๋ ฅ
  • tkinter.Entry: ํ…์ŠคํŠธ ํ•œ ์ค„ ์ž…/์ถœ๋ ฅ, ex) ์•„์ด๋””, ์•”ํ˜ธ
  • tkinter.Label: ์ด๋ฏธ์ง€, ํ…์ŠคํŠธ ์ถœ๋ ฅ
  • tkinter.Scrollbar
  • tkinter.Frame
  • tkinter.PannedWindow
  • tkinter.TopLevel: ์ƒˆ ์ฐฝ

Layout

  • tkinter.widget.place: ์ ˆ๋Œ€ ์œ„์น˜, grid๋‚˜ pack๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • tkinter.widget.pack: ์ƒ๋Œ€ ์œ„์น˜
  • tkinter.widget.grid: ๊ฒฉ์ž

bind

  • tkinter.widget.command: ๋ฒ„ํŠผ ๋“ฑ ์ผ๋ถ€ ์œ„์ ฏ์—๋งŒ ์žˆ์Œ
  • tkinter.widget.bind: ์œ„์ ฏ์—์„œ ์ผ์–ด๋‚˜๋Š” ํŠน์ • event์— ๋งž์ถฐ ์ •ํ•ด์ง„ ํ•จ์ˆ˜ ์‹คํ–‰

messagebox

import tkinter.messagebox
  • tkinter.messagebox.showinfo
  • tkinter.messagebox.showwarning
  • tkinter.messagebox.showerror
  • tkinter.messagebox.askquestion
  • tkinter.messagebox.askokcancel
  • tkinter.messagebox.askretrycancel
  • tkinter.messagebox.askyesno
  • tkinter.messagebox.askyesnocancel

Examples

Basic

import tkinter as tk
import logging
logging.basicConfig(
format="[%(levelname)-8s] %(lineno) 4d line : %(message)s",
level=logging.CRITICAL,
)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
class UiMainWindow(tk.Frame):
def __init__(self, master=None):
super().__init__(master=master)
self.pack()
def setup_ui(self):
self.master.title("Basic")
self.master.resizable(False, False)
"""
UI ์„ค์ •
"""
class MainWindow(UiMainWindow):
def __init__(self, master=None):
super().__init__(master=master)
self.setup_ui()
"""
๊ธฐ๋Šฅ
bind, command
"""
if __name__ == "__main__":
app = tk.Tk()
main_window = MainWindow(app)
app.mainloop()

Test

import tkinter
import tkinter.messagebox
import threading
import queue
class UiMainWindow(tkinter.Frame):
def __init__(self, master=None):
super().__init__(master=master)
self.pack()
def setup_ui(self):
# master๋Š” UiMainWindow Frame์„ ๊ด€๋ฆฌํ•˜๋Š” ์ƒ์œ„ ๊ฐ์ฒด
self.master.title("Main Window")
# ์‚ฌ์ด์ฆˆ ์กฐ์ • ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ๋„ˆ๋น„, ๋†’์ด
self.master.resizable(True, True)
# ํ™”๋ฉด ํฌ๊ธฐ
width = self.winfo_screenwidth()
height = self.winfo_screenheight()
# ์ฐฝ ํฌ๊ธฐ
win_width = 220
win_height = 100
# "๋„ˆ๋น„x๋†’์ด+x์ขŒํ‘œ+y์ขŒํ‘œ"
self.master.geometry(
"{}x{}+{}+{}".format(
win_width,
win_height,
(width - win_width) // 2,
(height - win_height) // 2,
)
)
# ํ…์ŠคํŠธ ์ž…๋ ฅ์ฐฝ ์ถ”๊ฐ€
self.text_1 = tkinter.Text(self, width=30, height=5)
# ๋ฒ„ํŠผ ์ถ”๊ฐ€
self.button_1 = tkinter.Button(self, text="๋ชจ๋‘ ์‚ญ์ œ", width=13)
self.button_2 = tkinter.Button(self, text="์‚ญ์ œ ํ™•์ธ", width=13)
# widget ์œ„์น˜ ์„ค์ •
self.text_1.grid(row=0, column=0, columnspan=2)
self.button_1.grid(row=1, column=0)
self.button_2.grid(row=1, column=1)
class MainWindow(UiMainWindow):
def __init__(self, master=None):
super().__init__(master=master)
self.background_loop = BackgroundThread()
self.background_loop.start()
# UI ์„ค์ •
self.setup_ui()
# ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
self.button_1.bind("<Button-1>", self.push_button_1)
self.button_2.bind("<Button-1>", self.push_button_2)
def push_button_1(self, event):
# ์ฒซ๋ฒˆ์งธ ์ค„ 0๋ฒˆ ์œ„์น˜๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์‚ญ์ œ
self.text_1.delete(1.0, tkinter.END)
def push_button_2(self, event):
self.background_loop.add(func=self.delete_message)
def delete_message(self):
text = self.text_1.get(1.0, tkinter.END)
# yes or no ๋ฉ”์‹œ์ง€ ๋ฐ•์Šค
answer = tkinter.messagebox.askyesno(
"๋ฉ”์‹œ์ง€ ์‚ญ์ œ", '"{}" ๋ฅผ ์ง€์šธ๊นŒ์š”?'.format(text[:-1])
)
if answer == True:
self.text_1.delete(1.0, tkinter.END)
class BackgroundThread(threading.Thread):
def __init__(self):
super().__init__()
self.daemon = True
self._queue = queue.Queue()
def run(self):
while True:
func, args = self._queue.get()
func(*args)
def add(self, func=None, args=()):
if func is not None:
self._queue.put((func, args))
if __name__ == "__main__":
app = tkinter.Tk()
main_window = MainWindow(app)
app.mainloop()
Last updated on