【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()

 

代碼示例圖: 

 

發佈了79 篇原創文章 · 獲贊 64 · 訪問量 8194
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章