立即學習:https://edu.csdn.net/course/play/19711/343116?utm_source=blogtoedu
復選框Checkbutton:與單選框是相對的,一些用法都是類似的,見單選框
?
注:在設置復選框的title和index時,設置為選中時onvalue = 1,未選中offvalue = 0
?
完整代碼
import tkinter#導入創建窗體的相關模塊
import osimage_path = r'C:\Users\jinlin\Desktop\python_further_study\GUI編程\resources' + os.sep + 'linlianqin.gif'#因為每個平臺的分隔符不一樣,所以用os.sep可以自動切換到相應平臺的分隔符class Mainwindow():#創建窗口類def __init__(self):self.root = tkinter.Tk() # 創建主體窗口self.root.title('linlianqin') # 定義窗體的名字self.root.geometry('500x500') # 定義窗體的初始大小self.root.maxsize(1200, 1200) # 設置窗口可以顯示的最大尺寸self.checkbutton()self.root.mainloop() #顯示窗口,這個代碼一定要放在所有窗口設置的后面def checkbutton(self):#定義復選框self.title_lable = tkinter.Label(self.root,text = "選擇您擅長的語言:")self.title_lable.pack(anchor="w")self.language = [("java",tkinter.IntVar()),("python",tkinter.IntVar()),("c",tkinter.IntVar()),("c#",tkinter.IntVar()),("c++",tkinter.IntVar()),("PHP",tkinter.IntVar()),("VB",tkinter.IntVar()),("HTML",tkinter.IntVar()),]#設置復選框的內容,tkinter.InVar()是為了默認選項進行使用的for title,status in self.language:self.check_button = tkinter.Checkbutton(self.root,text = title,variable = status,onvalue = 1,offvalue = 0,#選中為1,未選中為0command = self.checkbutton_event)self.check_button.pack(anchor = "w")self.content = tkinter.StringVar()self.show_label = tkinter.Label(self.root, textvariable=self.content)self.show_label.pack(anchor="w")def checkbutton_event(self):text = "您擅長的領域為:"for title,status in self.language:if status.get() == 1:text += title + "、"self.content.set(text)if __name__ == '__main__':Mainwindow()#將窗體類實例化
?