GUI ultimate choice: Tkinter3: Checkbutton, Radiobutton and LabelFrame.

Checkbutton ##
   checkbutton : checkbutton is our common checkbox button
       (write below a "turn sign" of the program, for example)
   prior to test Checkbutton, to feel its usage

from tkinter import *

root = Tk()

v = IntVar()

c = Checkbutton(root,text="测试一下",variable = v)
c.pack()


mainloop()

Here Insert Picture Description                 Here Insert Picture Description
       (Analysis: First, to instantiate a root or top-level window, then v = IntVar () where Tkinter need to use a variable, indicating whether or not this button is selected, as a feedback, then checkbutton instantiated, i.e., the first parameter the buttons on the root window, and the second parameter is the content of a button, and the third parameter is the variable parameter setting v equal to that variable, and so on back to whether the feedback button is selected)

   In order to be more intuitive to see the performance of selected and not selected when the state of v, we can show it, show the label Label inside.
Here Insert Picture Description
Here Insert Picture Description                 Here Insert Picture Description

   Flop began to write program

from tkinter import *

root = Tk()

GIRLS = ["戴安娜","九尾狐妖","卡特","辛德拉"]

v = []

for girl in GIRLS:
    v.append(IntVar())
    b = Checkbutton(root,text = girl,variable = v[-1])
    b.pack()

mainloop()

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description

       (Analysis: The same initialize a root window, and then use this cycle, then make our code even more concise, so first-come GIRLS list, and then come back a variable v, we need each button a variable store, so go to a list, and the like may be added to the cycle, and then inside the loop, each time a Tkinter variable for storing the state of each button, it can be added to the list, and then instantiate Checkbutton button, third argument because every time he got the last element, it is -1)

   You can see the button option is not neat, we formulate the position of the pack by setting the anchor options.
       (Anchor is a control text (or image) the position shown in Button he parameter values are set in 9 Method: "n", "ne" , "e", "se", "s", "sw" , "w", "nw" , "center", they are the Old and abbreviations ewsn represent the East and West, and then get hold of here left on it, pay attention to the time to write uppercase )
Here Insert Picture Description
Here Insert Picture Description

Radiobutton ##
   Radiobutton : Radiobutton is radio buttons
       (Radiobutton usage and usage Checkbutton basically the same, the only difference is Radiobutton to achieve the effect of the radio, then to achieve this effect mutually exclusive, is the need in the same group All Radiobutton can only share a variable option, and the need to set the value of the different value options.)

from tkinter import *

root = Tk()

v = IntVar()

Radiobutton(root,text = "One",variable = v,value=1).pack(anchor =W)
Radiobutton(root,text = "Two",variable = v,value=2).pack(anchor =W)
Radiobutton(root,text = "Three",variable = v,value=3).pack(anchor =W)

mainloop()

Here Insert Picture Description
       (解析:一样的,先来个root窗口,然后上面说了多个按钮只需要一个变量,所以v = IntVar()就不用列表了,然后实例化出三个Radiobutt,然后第三个参数他们的variable的值都等于v,共享一个,接着后面要有value,而且他们的值不可以相同,在pick里面在设置一下左对齐。
       然后上面有两次需要注意的,variable只能设置为同样一个变量,设置为v,然后value的值一定要不同才能实现互斥,
       这个原理其实就是,每一次点中一个,比如点中了Two,那么他会把按钮的值即value =2赋给了v,给了这个v就说明他点中了,和其他两个一对比,上面的variable的值不少,下面的variable的值不是3,所以呢他们两个就不显示,v是等于2,和value=2是匹配的,这样就实现了互斥,单点了
       如果上面那个value也是2的话,那么问题就来了,只要点One和Two其中一个,两个就会同时显示)

       如果有多个选项的话,我们仍然可以使用循环,使得代码更加简洁:

from tkinter import *

root = Tk()

LANGS = [
    ("Python",1),
    ("Perl",2),
    ("Ruby",3),
    ("Lua",4)]


v = IntVar()
v.set(1)

for lang, num in LANGS:
    b = Radiobutton(root,text=lang,variable=v,value=num)
    b.pack(anchor = W)


mainloop()

Here Insert Picture Description

   ※ 我们可以通过在Radiobutton的参数里设置:indicatoron = False,indicatoron 就是指示器,就是前面的小圆圈,设置为False就可以去掉小圈圈,变成按钮的形式
Here Insert Picture Description
Here Insert Picture Description

       但是这样不太美观,我们可以在pack里设置fill=X就是横向填充按钮,如果设置fill=Y就是纵向填充。
Here Insert Picture Description
Here Insert Picture Description
##LabelFrame
   LavelFrame:标签框架,这实际上是 Frame 框架的进化版,从形态上来说,也就是添加了 Label 的 Frame,但是有了它,Checkbutton 和 Radiobutton 的分组就变得简单了。

       比如现在想把刚才代码的四个组件给放到一个Frame里边去:

Here Insert Picture Description

from tkinter import *

root = Tk()

group = LabelFrame(root,text="最好的脚本语言是?",padx = 5,pady=5)
group.pack(padx=10,pady =10)

LANGS = [
    ("Python",1),
    ("Perl",2),
    ("Ruby",3),
    ("Lua",4)]


v = IntVar()

for lang, num in LANGS:
    b = Radiobutton(group,text=lang,variable=v,value=num)
    b.pack(anchor =W)


mainloop()

Here Insert Picture Description

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104148732