一、参数说明
| 语法 | 作用 |
|---|---|
| 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()
三、效果图
