【tkinter模块04】多选框控件

import tkinter

# 创建主窗口
win = tkinter.Tk()

# 设置标题
win.title('选择题')

# 设置大小和位置,前两个x大小 后两个+位置
win.geometry('400x400+500+200')

check1 = tkinter.Checkbutton(win, text='选项A')
check1.pack()

check2 = tkinter.Checkbutton(win, text='选项B')
check2.pack()

check3 = tkinter.Checkbutton(win, text='选项C')
check3.pack()

# 进入消息循环
win.mainloop()

代码示例图:

将选中的选项打印在Text控件中

import tkinter

# 创建主窗口
win = tkinter.Tk()

# 设置标题
win.title('选择题')

# 设置大小和位置,前两个x大小 后两个+位置
win.geometry('400x400+500+200')


def updata():
    message = ''
    if hobby1.get():
        message += '权利\n'
    if hobby2.get():
        message += '金钱\n'
    if hobby3.get():
        message += '女人\n'

    # 清除text中的所有内容(从头到尾)
    text.delete(0.0,tkinter.END)
    # 插入到文本框中
    text.insert(tkinter.INSERT, message)


# 要绑定的变量 布尔类型
hobby1 = tkinter.BooleanVar()

check1 = tkinter.Checkbutton(win, text='选项A', variable=hobby1, command=updata)
check1.pack()
hobby2 = tkinter.BooleanVar()
check2 = tkinter.Checkbutton(win, text='选项B', variable=hobby2, command=updata)
check2.pack()
hobby3 = tkinter.BooleanVar()
check3 = tkinter.Checkbutton(win, text='选项C', variable=hobby3, command=updata)
check3.pack()

text = tkinter.Text(win, width=30, height=4)
text.pack()
# 进入消息循环
win.mainloop()

代码示例图: 

 

发布了96 篇原创文章 · 获赞 103 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38114487/article/details/104393848
今日推荐