1、Radiobutton的基本屬性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * def event(): print('選中的組件值為:{}'.format(value.get())) if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南風丶輕語') # 標題 screenwidth = win.winfo_screenwidth() # 屏幕寬度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 value = IntVar() value.set(0) # 不設置默認為0 radiobutton = Radiobutton( master=win, # 父容器 text='標簽', # 文本 bg='yellow', # 背景顏色 fg='red', # 文本顏色 activebackground='pink', # 狀態為active時的背景顏色 activeforeground='blue', # 狀態為active的文字顏色 relief='flat', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=1, # 高度 width=10, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 value=1, # 默認value,選中后會傳遞給variable variable=value, # 通過IntVar設置1為選中,0為未選中 command=event, # 點擊時的事件 ) radiobutton.pack() radiobutton = Radiobutton( master=win, # 父容器 text='標簽', # 文本 bg='pink', # 背景顏色 fg='blue', # 文本顏色 activebackground='yellow', # 狀態為active時的背景顏色 activeforeground='red', # 狀態為active的文字顏色 relief='flat', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=1, # 高度 width=10, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 value=2, # 默認value,選中后會傳遞給variable variable=value, # 通過IntVar設置1為選中,0為未選中 command=event, # 點擊時的事件 ) radiobutton.pack() radiobutton = Radiobutton( master=win, # 父容器 text='標簽', # 文本 bg='yellow', # 背景顏色 fg='red', # 文本顏色 activebackground='pink', # 狀態為active時的背景顏色 activeforeground='blue', # 狀態為active的文字顏色 relief='flat', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。 bd=3, # 邊框的大小 height=1, # 高度 width=10, # 寬度 padx=1, # 內間距,字體與邊框的X距離 pady=1, # 內間距,字體與邊框的Y距離 state='normal', # 設置狀態 normal、active、 disabled cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus... font=('黑體', 20), # 字體 value=3, # 默認value,選中后會傳遞給variable variable=value, # 通過IntVar設置1為選中,0為未選中 command=event, # 點擊時的事件 ) radiobutton.pack() Label(win, textvariable=value, relief='g', width=10).pack(padx=20) # 用於顯示value的值 win.mainloop()
備注:
①支持的字體(通過tkinter.font.families獲取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/
②鼠標樣式選項
"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus","shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"
③邊框樣式,組件狀態閱覽
2、分組的RadioButton
保證RadioButton不在同一個父元素下,則可以進行區分