Tkinter的Radiobutton控件

简介: Tkinter的Radiobutton是一个含有多个选项的控件,但是只能选择其中的一个选项

使用方法

R1=tk.Radiobutton(root,text='A',variable=var,value='A',command=printf)
R1.pack()
R2=tk.Radiobutton(root,text='B',variable=var,value='B',command=printf)
R2.pack()
R3=tk.Radiobutton(root,text='C',variable=var,value='C',command=printf)
R3.pack()

该程序创建了三个选项A,B,C;root表示界面,text表示选项的标签,variable表示选择R1时得到的值,value为值,command为函数;

完整程序

import tkinter as tk
root=tk.Tk()
root.title('Radiobutton')
screenwidth=root.winfo_screenwidth()
screenheight=root.winfo_screenheight()
width=500
height=300
x=(screenwidth-width)//2
y=(screenheight-height)//2
root.geometry('%dx%d+%d+%d'%(width,height,x,y))
var=tk.StringVar()
L=tk.Label(root,bg='white',width=20,text='       ')
L.place(x=180,y=50)
def printf():
    L.config(text=var.get())
R1=tk.Radiobutton(root,text='A',variable=var,value='A',command=printf)
R1.place(x=230,y=100)
R2=tk.Radiobutton(root,text='B',variable=var,value='B',command=printf)
R2.place(x=230,y=150)
R3=tk.Radiobutton(root,text='C',variable=var,value='C',command=printf)
R3.place(x=230,y=200)
root.mainloop()

运行结果

1.png

目录
相关文章
|
9月前
|
C# 数据库 开发者
44.c#:combobox控件
44.c#:combobox控件
120 1
|
9月前
|
Python
tkinter之Scrollbar滚动条
tkinter之Scrollbar滚动条
108 1
|
6月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
208 2
|
6月前
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
970 1
|
9月前
|
数据处理 C# UED
42.c#:progressbar控件
42.c#:progressbar控件
115 1
|
9月前
|
Python
tkinter之Radiobutton
tkinter之Radiobutton
58 1
|
9月前
|
Python
tkinter之Combobox复选框
tkinter之Combobox复选框
198 2
|
9月前
|
Python
tkinter滚动条
tkinter滚动条
50 1
|
9月前
|
Python
tkinter之button添加背景图片
tkinter之button添加背景图片
187 1
|
9月前
|
Python
tkinter之Button按钮
tkinter之Button按钮
60 1