본문 바로가기
파이썬

tkinter 버튼 함수와 관련된 것

by 설화_ 2024. 4. 7.
import tkinter as tk

# Tkinter 윈도우 생성
window = tk.Tk()

button = tk.Button(window, text="Button")
button.pack()

# 이벤트 루프 시작
window.mainloop()

이 코드는 버튼을 만드는 코드이다 하지만 가끔씩 버튼 태두리 때문에 불편하지 않은가?

그럴때는 저 코드를 

import tkinter as tk

# Tkinter 윈도우 생성
window = tk.Tk()

button = tk.Button(window, text="Button", bd = 0, highlightthickness = 0)
button.pack()

# 이벤트 루프 시작
window.mainloop()

이런 식으로 bd = 0, highlightthickness = 0 이 옵션을 써주길 바란다.

그 다음 이번에는 버튼을 누르면 버튼이 사라지는 걸 만들어볼 것이다

import tkinter as tk

# Tkinter 윈도우 생성
window = tk.Tk()

def del_button():
    button.destroy()

button = tk.Button(window, text="Button", bd = 0, highlightthickness = 0, command=del_button)
button.pack()

# 이벤트 루프 시작
window.mainloop()

이런 식으로 destory를 사용하면 버튼을 누를 시 버튼이 사라지도록 만들 수 있다. 

이번에는 버튼 색이 너무 칙칙하지 않은가? 이번엔 버튼색이 바뀌도록 해볼 것이다.

import tkinter as tk

# Tkinter 윈도우 생성
window = tk.Tk()

def del_button():
    button.destroy()

button = tk.Button(window, text="Button", bd = 0, highlightthickness = 0, command=del_button, bg='blue')
button.pack()

# 이벤트 루프 시작
window.mainloop()

이런 식으로 하면 버튼의 색이 파란색 으로 바뀐다 바로 bg 이 옵션이다 

import tkinter as tk

# Tkinter 윈도우 생성
window = tk.Tk()

def del_button():
    button.destroy()

button = tk.Button(window, text="Button", bd = 0, highlightthickness = 0, command=del_button, bg='blue', width=10, height=10)
button.pack()

# 이벤트 루프 시작
window.mainloop()

여기선 버튼의 크기를 조정한다. width=10, height=10 이 부분이다 숫자를 늘리면 버튼이 커지고 줄이면 작아진다.

 

질문이나 문의 사항 있으시면 언제나 댓글 쪽지로 남겨주세요!

그럼 오늘의 포스팅 마치도록 하겠습니다~!

'파이썬' 카테고리의 다른 글

pyautogui 사용법  (0) 2024.04.10
파이썬 tkinter의 기본 함수 설명  (0) 2024.03.23