Python tkinter之Radiobutton(单选)

原文网址:https://www.cnblogs.com/rainbow-tan/p/14103525.html

# -*- 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()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值