10. Chekbutton holatini sozlash
Bu erda biz BooleanVar tipidagi o'zgarmaydiganni yaratamiz, bu standart
Python o'zgarmaydigan emas, bu Tkinter o'zgaruvchisi, keyin yuqoridagi misolda
tasdiq qutisi holatini true qilib belgilash uchun uni Checkbutton sinfiga o'tkazamiz.
Siz tasdiqlash qutisi belgilanmagan bo'lishi uchun BooleanVar -ni false
qiymatiga qo'yishingiz mumkin.
Bundan tashqari, BooleanVar o'rniga IntVar dan foydalaning va qiymatlarni 0
va 1 ga o'rnating.
140
chk_state = IntVar()
chk_state.set(0) # False
chk_state.set(1) # True
Bu misollar BooleanVar bilan bir xil natijani beradi.
RadioButton vidjetlarini qo'shish
Radio tugmalarini qo'shish uchun RadioButton sinfidan foydalaning:
rad1 = Radiobutton(window,text='Salom', value=1)
from tkinter import *
from tkinter.ttk Radiobutton *
window =Tk()
window.title(“Mening birinchi grafik oyman”)
window.geometry(‘350x150’)
radio1=Radiobutton(window, text= ‘Birinchi’, value=1)
radio2=Radiobutton(window, text= ‘Ikkinchi’, value=2)
radio3=Radiobutton(window, text= ‘Uchinchi’, value=3)
radio1.grid(column=0, row=0)
radio2.grid(column=1, row=0)
radio3.grid(column=2, row=0)
window.mainloop()
141
11. Radio tugmachasining qiymatini olish
Tanlangan radio tugmani yoki uning qiymatini olish uchun parametrni
o'zgaruvchiga o'tkazib, uning qiymatini olish mumkin.
from tkinter import *
from tkinter.ttk Radiobutton *
def tanlash():
label.configure(text=selected.get())
window =Tk()
window.title(“Mening birinchi grafik oyman”)
window.geometry(‘350x150’)
selected=IntVar()
radio1=Radiobutton(window, text= ‘Birinchi’, value=1, variable=selected)
radio2=Radiobutton(window, text= ‘Ikkinchi’, value=2, variable=selected)
radio3=Radiobutton(window, text= ‘Uchinchi’, value=3, variable=selected)
button=Button(window, text= “Natija”, command=tanlash)
label=Label(window)
radio1.grid(column=0, row=0)
radio2.grid(column=1, row=0)
radio3.grid(column=2, row=0)
button.grid(column=3, row=0)
label.grid(column=0, row=1)
window.mainloop()
142
Har safar radio tugmachasini tanlaganingizda, o'zgaruvchining qiymati tugma
qiymatiga o'zgartiriladi.
Dostları ilə paylaş: |