본문 바로가기

컴퓨터/Python

Python tkinter

▒ 설치

tkinter 는 파이썬의 내장 모듈이므로 따로 설치할 필요가 없다.

▒ 가져오기

import tkinter as tk

▒ 창

1) 생성

window = tk.Tk()

2) 타이틀

window.title('창 이름');

3) 크기

window.geometry("300x200")
window.minsize(200, 100)
window.maxsize(1820, 1080)
window.resizable(False, False)

 위젯

#추가
위젯.pack()
#변경
위젯.config(option=value)
#숨김
위젯.pack_forget()
#삭제
위젯.destroy()

라벨

label = tk.Label(window, text='Hello World')

 입력창

entry = tk.Entry(window)
entry.insert(0, "텍스트") #0은 텍스트 삽입 위치
entry.get() #값 가져오기

StringVar() 객체

text_var = tk.StringVar()
entry = tk.Entry(window, textvariable=text_var)

# 나중에 StringVar 객체를 통해 텍스트 변경
text_var.set("새로운 텍스트")

 버튼

def on_button_click():
    print("Button clicked!")

button = tk.Button(window, text="Click Me", command=on_button_click)

  실행

window.mainloop()

'컴퓨터 > Python' 카테고리의 다른 글

Python file  (0) 2023.12.04
Python pyinstaller  (1) 2023.12.04
Python Collections | 파이썬 콜렉션  (0) 2023.02.04
Python tuple | 파이썬 튜플  (0) 2023.02.01
Python max() | 파이썬 최대값  (0) 2023.02.01