본문 바로가기
SW 코딩/GUI, tkinter

[파이썬 python] tkinter - 체크버튼 check button

2021. 9. 30.
반응형

 

 

참고사이트: 유튜브 나도코딩

code - 체크버튼 check button

6. 체크버튼 check button.txt
0.00MB

 

code 설명

import tkinter as tk

root = tk.Tk()  # tkinter root창 생성

root.title("계산기") #창 이름
root.geometry("500x500+200+200") # 창 크기, 가로 x 세로 + 창 출력 위치 좌표

'''
checkvar에 int 형으로 값 저장
선택상태가 무엇인지 return 받기 위해 사용
'''
checkvar1 = tk.IntVar()

checkbox = tk.Checkbutton(root, text="오늘 하루 보지 않기", variable=checkvar1)
#checkbox.select() #기본값 선택해놓기
checkbox.deselect() #기본값 선택하지 않기
checkbox.pack()

checkvar2 = tk.IntVar()

checkbox2 = tk.Checkbutton(root, text="일주일동안 보지 않기", variable=checkvar2)
checkbox2.pack()


def button_command():
    print(checkvar1.get()) # 0: 체크 해제, 1: 체크 상태
    print(checkvar2.get()) # 0: 체크 해제, 1: 체크 상태

btn = tk.Button(root, text = "클릭1", command=button_command)
btn.pack()

btn2 = tk.Button(root, text = "클릭2", command=button_command)
btn2.pack()

root.mainloop()

 

결과 화면

댓글


loading