• TKinter Radiobutton


    TKinter Radiobutton

    觉得有用的话,欢迎一起讨论相互学习~Follow Me

    感谢莫烦老师
    详情

    创建windows

    window = tk.Tk()
    window.title('my window')
    window.geometry('500x500')
    

    创建显示label

    var1 = tk.StringVar()
    l = tk.Label(window, bg='yellow', width=4, textvariable=var1)
    l.pack()
    

    循环刷新窗口

    window.mainloop()  # 不断刷新主窗口
    

    显示函数

    def print_selection():
        l.config(text='you have selected ' + var.get())
    

    Radiobutton

    # 以下意为Radiobutton在window控件上
    # 显示为Option A
    # 选择时更改变量var的值为A
    # 并且会激活print_selection函数
    r1 = tk.Radiobutton(window, text='Option A',
                        variable=var, value='A',
                        command=print_selection)
    r1.pack()
    r2 = tk.Radiobutton(window, text='Option B',
                        variable=var, value='B',
                        command=print_selection)
    r2.pack()
    r3 = tk.Radiobutton(window, text='Option C',
                        variable=var, value='C',
                        command=print_selection)
    r3.pack()
    

    在这里插入图片描述

    完整代码

    # View more python learning tutorial on my Youtube and Youku channel!!!
    
    # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
    # Youku video tutorial: http://i.youku.com/pythontutorial
    
    import tkinter as tk
    
    window = tk.Tk()
    window.title('my window')
    window.geometry('500x500')
    
    var = tk.StringVar()
    l = tk.Label(window, bg='yellow', width=20, text='empty')
    l.pack()
    
    
    def print_selection():
        l.config(text='you have selected ' + var.get())
    
    
    # 以下意为Radiobutton在window控件上
    # 显示为Option A
    # 选择时更改变量var的值为A
    # 并且会激活print_selection函数
    r1 = tk.Radiobutton(window, text='Option A',
                        variable=var, value='A',
                        command=print_selection)
    r1.pack()
    r2 = tk.Radiobutton(window, text='Option B',
                        variable=var, value='B',
                        command=print_selection)
    r2.pack()
    r3 = tk.Radiobutton(window, text='Option C',
                        variable=var, value='C',
                        command=print_selection)
    r3.pack()
    
    window.mainloop()
    
    
  • 相关阅读:
    又是一个递归的题
    全文检索引擎记录
    处理“System.Web.HttpException: 超过了最大请求长度”
    完整国内城市js级联选择
    多服务器共享session的解决方案之一
    递归求n的阶乘
    面试遇到了 <计算从1到N中1的出现次数>结果悲剧了.
    DropDownList绑定Xml数据的实现
    简单图片上传示例
    C#默认访问修饰符
  • 原文地址:https://www.cnblogs.com/cloud-ken/p/12615963.html
Copyright © 2020-2023  润新知