zoukankan      html  css  js  c++  java
  • 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()
    
    
  • 相关阅读:
    UIPickerView-一.01-
    闭包-01-Swift
    Swift 入门-01-概述
    git使用命令行-01-自己操作的
    FetchedResultsController-03-CoreData相关
    SQLite-05-增删改查
    Sqlite函数总结-04
    List<string>转xml
    比较两个List<T>是否相同
    获取DataTable前几条数据
  • 原文地址:https://www.cnblogs.com/cloud-ken/p/12615963.html
Copyright © 2011-2022 走看看