파이썬 (pythoon)

[python] tkinter / checkbox / 체크박스

working for you 2021. 7. 16. 12:45
반응형

1. Checkbutton  / 체크박스

2. IntVar() : 변수 타입지정 정수형 (필수)

3. variable=var : 타입지정 변수 적용

4. button command = show 함수 호출

5. Label에 체크박스(0/기본, 1/첫번째체크박스값) 표기하기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from tkinter import *
 
root = Tk()
root.title('checkbox study')
root.geometry("400x400")
 
def show():
    Label(root, text=var.get()).pack()
 
var = IntVar()
 
= Checkbutton(root, text='체크박스', variable=var)
c.pack()
 
my_button = Button(root, text='클릭', command=show).pack()
my_label = Label(root, text=var.get()).pack()
 
 
root.mainloop()
cs

[지정한 문자값 받아오기]

1. StringVal() : 변수 타입지정 문자형 (필수)

2. Checkbutton 속성

# onvalue = 'on'  : 체크박스가 선택되어 있을때 'on' 값을 리턴

# offvalue = 'off'  : 체크박스가 해제되어 있을때 'off' 값을 리턴

# 아무것도 선택이 안되어 있을때 이벤트 발생시 '0'을 문자형으로 리턴

3. deselect() : 체크박스 기본값을 해제 (이것을 하지 않으면 체크오류가 있음)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from tkinter import *
 
root = Tk()
root.title('checkbox study')
root.geometry("400x400")
 
def show():
    Label(root, text=var.get()).pack()
 
var = StringVar()
 
= Checkbutton(root, text='체크박스', variable=var, onvalue='on', offvalue='off')
c.deselect()
c.pack()
 
my_button = Button(root, text='클릭', command=show).pack()
my_label = Label(root, text=var.get()).pack()
 
 
root.mainloop()
cs
반응형