美文网首页
16.图形交互界面之Tkinter-checkbutton多选

16.图形交互界面之Tkinter-checkbutton多选

作者: DonkeyJason | 来源:发表于2018-07-12 20:54 被阅读0次

    案例1

    #实现多选Checkbutton
    from tkinter import *
    
    
    root = Tk()
    
    
    GIRLS = ['西施','貂蝉','王昭君','杨玉环']
    
    v = []
    
    for girl in GIRLS:
        v.append(IntVar())
        b = Checkbutton(root,text = girl, variable = v[-1])
        b.pack(anchor = W)
    
    
    mainloop()   
    

    类的形式

    #实现多选Checkbutton
    
    from tkinter import *
    import math as m
    class App(Frame):
        def __init__(self,master = None):
            Frame.__init__(self,master)
            self.pack()
    
    
    
    # create the applilcation
    myapp = App()
    
    
    #
    # here are method calls to the window manger class
    #
    myapp.master.title("Checkbutton")
    myapp.master.maxsize(1000, 400)
    
    
    
    
    GIRLS = ['西施','貂蝉','王昭君','杨玉环']
    
    v = []
    
    for girl in GIRLS:
        v.append(IntVar())
        b = Checkbutton(myapp,text = girl, variable = v[-1])
        b.pack(anchor = W)
    
    mainloop()
    
    image.png

    案例2

    • for实现循环选择
    import tkinter as tk
     
    window=tk.Tk()
    window.title('checkbutton')
    window.geometry('400x400')
     
     
    #定义label用于显示
    l=tk.Label(window,
                bg='red',
                width=40,height=2,
                text='u have not chosen anything yet')
    l.pack()
     
    def print_selection():
        if (op_mark[0].get()==1)&(op_mark[1].get()==1):
            l.config(text='你怎么两个人通吃啊!')
        elif(op_mark[0].get()==1)&(op_mark[1].get()==0):
            l.config(text='啊哈!我也喜欢成成!')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==1):
            l.config(text='谁让你喜欢冉冉的,成成才能喜欢冉冉!')
        else:
            l.config(text='快选啊~我还等着你呢、')
     
     
    ops=['成成','冉冉']
    op_mark=[tk.IntVar(),tk.IntVar()]
    #这里注意onvalue和offvalue不能改名字,只能用这两个名字!我就被坑了。。
    for i in range(len(ops)):
        cb=tk.Checkbutton(window,
                       text=ops[i],
                       variable=op_mark[i],
                       offvalue=0,
                       command=print_selection)
        cb.pack()
    
    image.png
    • 没有for
    import tkinter as tk
     
    window=tk.Tk()
    window.title('checkbutton')
    window.geometry('400x400')
     
     
    #定义label用于显示
    l=tk.Label(window,
                bg='yellow',
                width=40,height=2,
                text='u have not chosen anything yet')
    l.pack()
    def print_selection():
        if (var1.get()==1)&(var2.get()==1)&(var3.get()==1):
             l.config(text='你怎么三个人通吃啊!')
        elif(var1.get()==1)&(var2.get()==0)&(var3.get()==0):
             l.config(text='啊哈!我也喜欢pig!')
        elif(var1.get()==0)&(var2.get()==0)&(var3.get()==1):
             l.config(text='dog!我才不喜欢dog!')
        elif(var1.get()==0)&(var2.get()==1)&(var3.get()==0):
             l.config(text='谁让你喜欢donkey的,pig才能喜欢donkey!')
        elif(var1.get()==1)&(var2.get()==1)|(var1.get()==1)&(var3.get()==1)|(var2.get()==1)&(var3.get()==1):
            l.config(text='你小子还不算贪心,才喜欢两个')
        else:
             l.config(text='快选啊~我还等着你呢、')
    ops=['pig','donkey','dog']
     
    var1=tk.IntVar()
    var2=tk.IntVar()
    var3=tk.IntVar()
    cb1=tk.Checkbutton(window,
                    text=ops[0],
                    variable=var1,#选定或不选定存放在var1这个变量里面。
                    onvalue=1,#如果选定了,那么var1的值就是1
                    offvalue=0,#如果没有选定,那var1的值就是0
                    command=print_selection)
    cb1.pack()
    cb2=tk.Checkbutton(window,
                    text=ops[1],
                    variable=var2,#选定或不选定存放在var2这个变量里面。
                    onvalue=1,#如果选定了,那么var2的值就是1
                     offvalue=0,#如果没有选定,那var2的值就是0
                     command=print_selection)
    cb2.pack()
     
    cb3=tk.Checkbutton(window,
                    text=ops[2],
                    variable=var3,#选定或不选定存放在var2这个变量里面。
                       onvalue=1,#如果选定了,那么var2的值就是1
                     offvalue=0,#如果没有选定,那var2的值就是0
                     command=print_selection)
    cb3.pack()
        
        
    
        
    tk.mainloop() 
    
    image.png
    • 类实现
    from tkinter import *
    
    class App(Frame):
        def __init__(self,master = None):
            Frame.__init__(self,master)
            self.pack()
    
    myapp = App()     
            
    myapp.master.title('checkbutton')
    myapp.master.geometry('400x400')
     
     
    #定义label用于显示
    l=tk.Label(myapp,
                bg='yellow',
                width=40,height=2,
                text='u have not chosen anything yet')
    l.pack()
    def print_selection():
        if (op_mark[0].get()==1)&(op_mark[1].get()==1)&(op_mark[2].get()==1):
             l.config(text='你怎么三个人通吃啊!')
        elif(op_mark[0].get()==1)&(op_mark[1].get()==0)&(op_mark[2].get()==0):
             l.config(text='啊哈!我也喜欢pig!')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==0)&(op_mark[2].get()==1):
             l.config(text='dog!我才不喜欢dog!你竟然喜欢dog')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==1)&(op_mark[2].get()==0):
             l.config(text='谁让你喜欢donkey的,pig才能喜欢donkey!')
        elif(op_mark[0].get()==1)&(op_mark[1].get()==1)|(op_mark[0].get()==1)&(op_mark[2].get()==1)|(op_mark[1].get()==1)&(op_mark[2].get()==1):
            l.config(text='你小子还不算贪心,才喜欢两个')
        else:
             l.config(text='快选啊~我还等着你呢、')
    ops=['pig','donkey','dog']
     
    op_mark=[tk.IntVar(),tk.IntVar(),tk.IntVar()]
    
    for i in range(len(ops)):
        cb=tk.Checkbutton(myapp,
                       text=ops[i],
                       variable=op_mark[i],
                       offvalue=0,
                       command=print_selection)
        cb.pack()
    tk.mainloop() 
    
    image.png
    • 加强版

    实现多选Checkbutton

    from tkinter import *
    import math as m
    class App(Frame):
        def __init__(self,master = None):
            Frame.__init__(self,master)
            self.pack()
    
    
    
    # create the applilcation
    myapp = App()
    
    
    #
    # here are method calls to the window manger class
    #
    myapp.master.title("Checkbutton")
    myapp.master.maxsize(1000, 400)
    
    #定义label用于显示
    l=tk.Label(myapp,
                bg='yellow',
                width=40,height=2,
                text='u have not chosen anything yet')
    l.pack()
    def print_selection():
        if(op_mark[0].get()==1)&(op_mark[1].get()==1)&(op_mark[2].get()==1)&(op_mark[3].get()==1)|\
        (op_mark[0].get()==1)&(op_mark[1].get()==1)&(op_mark[2].get()==0)&(op_mark[3].get()==1)|\
        (op_mark[0].get()==1)&(op_mark[1].get()==0)&(op_mark[2].get()==1)&(op_mark[3].get()==1)|\
        (op_mark[0].get()==0)&(op_mark[1].get()==1)&(op_mark[2].get()==1)&(op_mark[3].get()==1)|\
        (op_mark[0].get()==1)&(op_mark[1].get()==1)&(op_mark[2].get()==1)&(op_mark[3].get()==0):
            l.config(text='你这心太能装了,喜欢这么多!贪得无厌')
        elif(op_mark[0].get()==1)&(op_mark[1].get()==0)&(op_mark[2].get()==0)&(op_mark[3].get()==0):
             l.config(text='啊哈!我也喜欢西施!')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==1)&(op_mark[2].get()==0)&(op_mark[3].get()==0):
             l.config(text='貂蝉!我才不喜欢貂蝉!你竟然喜欢貂蝉,只有吕布能喜欢貂蝉')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==0)&(op_mark[2].get()==1)&(op_mark[3].get()==0):
             l.config(text='谁让你喜欢王昭君的,天子才能喜欢王昭君!')
        elif(op_mark[0].get()==0)&(op_mark[1].get()==0)&(op_mark[2].get()==0)&(op_mark[3].get()==1):
             l.config(text='谁让你喜欢王昭君的,天子才能喜欢王昭君!')
                
        elif(op_mark[0].get()==1)&(op_mark[1].get()==1)|(op_mark[0].get()==1)&(op_mark[2].get()==1)|(op_mark[1].get()==1)&(op_mark[2].get()==1)|(op_mark[0].get()==1)&(op_mark[3].get()==1)|(op_mark[1].get()==1)&(op_mark[3].get()==1)|(op_mark[0].get()==1)&(op_mark[3].get()==1)|(op_mark[1].get()==1)&(op_mark[3].get()==1)|(op_mark[2].get()==1)&(op_mark[3].get()==1):
            l.config(text='你小子还不算贪心,才喜欢两个')
        else:
             l.config(text='快选啊~我还等着你呢、')
    
    
    
    ops = ['西施','貂蝉','王昭君','杨玉环']
    op_mark =[tk.IntVar(),tk.IntVar(),tk.IntVar(),tk.IntVar()]
    
    
    for i in range(len(GIRLS)):
        cb=tk.Checkbutton(myapp,
                       text=ops[i],
                       variable=op_mark[i],
                       offvalue=0,
                       command=print_selection)
        
        cb.pack()
    
    mainloop()
    
    image.png

    相关文章

      网友评论

          本文标题:16.图形交互界面之Tkinter-checkbutton多选

          本文链接:https://www.haomeiwen.com/subject/srxdpftx.html