Radio button values in Python Tkinter

后端 未结 3 1666
陌清茗
陌清茗 2021-01-14 18:10

I am trying to create a dialog box in Python using Tkinter. The goal is to have a dialog box with two radio buttons and an \"OK\" button. Radio button one selects the opti

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 18:54

    As set the selection value will be set before the window appears (selection = 0).

    If you want to run tests after mainloop(), selection = var.get() should also be after mainloop() with tests.

    If you do not want to close the master window before tests, use command=function:

    from Tkinter import *
    
    def function():
        selection = var.get()
    
        if  selection == 1:
            # Default
    
        elif selection == 2:
            # User-defined
    
        else:#selection==0
            #No choice
    
        master.quit()
    
    master = Tk()
    var = IntVar()
    Label(master, text = "Select OCR language").grid(row=0, sticky=W)
    Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
    Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
    Button(master, text = "OK", command = function).grid(row=3, sticky=W)
    mainloop()
    

提交回复
热议问题