立即学习: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()#将窗体类实例化