[python] 클래스를 활용한 GUI 프로그래밍 예제

파이썬은 tkinter 라이브러리를 통해 GUI(GUI: Graphical User Interface)를 만들 수 있습니다. 이 예시에서는 클래스를 이용하여 간단한 GUI를 만드는 방법을 살펴보겠습니다.

예제 소스코드

다음은 간단한 라벨과 버튼을 포함한 GUI를 만드는 예제 소스코드입니다.

import tkinter as tk

class SimpleGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("간단한 GUI 예제")

        self.label = tk.Label(root, text="안녕하세요!")
        self.label.pack()

        self.button = tk.Button(root, text="클릭하세요", command=self.say_hello)
        self.button.pack()

    def say_hello(self):
        self.label.config(text="안녕하세요, 반가워요!")

if __name__ == "__main__":
    root = tk.Tk()
    gui = SimpleGUI(root)
    root.mainloop()

설명

이 예제를 실행하면 윈도우가 표시되고 “안녕하세요!”라는 라벨과 “클릭하세요”라는 버튼이 있습니다. 버튼을 클릭하면 라벨의 텍스트가 바뀝니다.

이처럼 파이썬을 이용하여 클래스 기반의 GUI 프로그래밍을 할 수 있습니다.

더 많은 정보는 tkinter 공식 문서를 참고하세요.