一、參數說明
語法 | 作用 |
---|---|
Radiobutton(root,text='xxxx') | 單選框文本顯示內容 |
Radiobutton(root,variable=color) | 單選框索引變量,通過變量的值確定哪個單選框被選中 |
Radiobutton(root,variable=color,value='red') | 單選框選中時設定變量的值 |
Radiobutton(root,variable=color,value='red',command=函數) | 單選框選中時執行的命令(函數) |
Radiobutton(root,indicatoron=False) | 設置單選框類型(默認為True) |
二、代碼示例
import tkinter as tk window = tk.Tk() def main(): global window # 設置主窗體大小 winWidth = 600 winHeight = 400 # 獲取屏幕分辨率 screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() # 計算主窗口在屏幕上的坐標 x = int((screenWidth - winWidth)/ 2) y = int((screenHeight - winHeight) / 2) # 設置主窗口標題 window.title("RadioButton參數說明") # 設置主窗口大小 window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y)) # 設置窗口寬高固定 window.resizable(0,0) # 設置窗口圖標 window.iconbitmap("./image/icon.ico") """radiobutton參數. Valid resource names: activebackground, activeforeground, anchor, background, bd, bg, bitmap, borderwidth, command, cursor, disabledforeground, fg, font, foreground, height, highlightbackground, highlightcolor, highlightthickness, image, indicatoron, justify, padx, pady, relief, selectcolor, selectimage, state, takefocus, text, textvariable, underline, value, variable, width, wraplength.""" # 設置默認選中 v=tk.IntVar() v.set(2) tk.Radiobutton(window, text="男", font=("Arial", 16), value=2, variable=v, indicatoron=False ).pack() tk.Radiobutton(window, text="女", font=("Arial", 16),value=1, variable=v, indicatoron=False ).pack() window.mainloop() def click(): print("click") if __name__ == '__main__': main()
三、效果圖