日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

学习笔记(52):Python实战编程-Radiobutton

發(fā)布時間:2023/12/10 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习笔记(52):Python实战编程-Radiobutton 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

立即學(xué)習(xí):https://edu.csdn.net/course/play/19711/343115?utm_source=blogtoedu

單選鈕:Radiobutton

?

1)相對于大部分組件而言,最大的區(qū)別在于單選鈕綁定事件是直接通過構(gòu)建單選鈕時方法中的command參數(shù)來進行事件的綁定,而其他的組件一般都是通過bind函數(shù)來進行事件的綁定。

self.singlebutton = tkinter.Radiobutton(self.root,text = title,value = index,command = self.singlebutton_handle_event,#通過command給單選鈕綁定事件variable = self.status)

2)單選鈕在創(chuàng)建時得注意的是:含有兩個值,一個是顯示的文本值,一個是真正執(zhí)行操作或者標記的值如下:

self.sex = [("男",0),("女",1)]#設(shè)置單選鈕要顯示的值以及真實操作的值

3)要想給單選鈕設(shè)置默認的選中項,則需要使用IntVar函數(shù),該函數(shù)可以通過.set()來設(shè)置初始的索引,在創(chuàng)建單選鈕的參數(shù)中使variable=.set()即可,也可以通過.get來獲得當前選擇項的索引

self.status = tkinter.IntVar()#設(shè)置默認選項self.status.set(0)self.singlebutton = tkinter.Radiobutton(self.root,text = title,value = index,command = self.singlebutton_handle_event,#通過command給單選鈕綁定事件variable = self.status)index == self.status.get():

4)完整代碼

import tkinter#導(dǎo)入創(chuàng)建窗體的相關(guān)模塊 import osimage_path = r'C:\Users\jinlin\Desktop\python_further_study\GUI編程\resources' + os.sep + 'linlianqin.gif'#因為每個平臺的分隔符不一樣,所以用os.sep可以自動切換到相應(yīng)平臺的分隔符class Mainwindow():#創(chuàng)建窗口類def __init__(self):self.root = tkinter.Tk()#創(chuàng)建主體窗口self.root.title('linlianqin')#定義窗體的名字self.root.geometry('500x500')#定義窗體的初始大小self.root.maxsize(1200,1200)#設(shè)置窗口可以顯示的最大尺寸self.single_button()self.root.mainloop()#顯示窗口,這個代碼一定要放在所有窗口設(shè)置的后面def single_button(self):#定義一個單選妞self.status = tkinter.IntVar()#設(shè)置默認選項self.label = tkinter.Label(self.root,text = "請選擇您的性別:")self.label.grid(row = 0,column = 0)self.sex = [("男",0),("女",1)]#設(shè)置單選鈕要顯示的值以及真實操作的值self.status.set(0)self.column = 1for title,index in self.sex:self.singlebutton = tkinter.Radiobutton(self.root,text = title,value = index,command = self.singlebutton_handle_event,#通過command給單選鈕綁定事件variable = self.status)self.singlebutton.grid(row = 0,column=self.column)self.column+=1def singlebutton_handle_event(self):#定義單選鈕的處理事件self.content = tkinter.StringVar()self.label = tkinter.Label(self.root,textvariable = self.content)for title,index in self.sex:if index == self.status.get():self.content.set("您選擇的性別是%s"%title)self.label.grid(row = 1,column = 0)if __name__ == '__main__':Mainwindow()#將窗體類實例化

?

總結(jié)

以上是生活随笔為你收集整理的学习笔记(52):Python实战编程-Radiobutton的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。