(已解决)Tkinter单选按钮不适用于IntVar

时间:2019-07-05 20:25:21

标签: python python-3.x tkinter radio-button

我正在尝试使用tkinter单选按钮获取此代码以返回难度值。我有一个问题,就是变量永远不会从0更改为难度。当我运行此代码时,控制台如下所示:

TEXT and VALUE Child's play 0.15
TEXT and VALUE Very Easy 0.3
TEXT and VALUE Easy 0.4
TEXT and VALUE Normal 0.5
TEXT and VALUE Hard 0.6
TEXT and VALUE Very Hard 0.7
TEXT and VALUE Insanely Hard 0.8
TEXT and VALUE Impossible 0.9

*我选择“非常困难”单选按钮,然后单击“确定” *

You selected the option 0

这是完整的代码:

import tkinter as tk
from tkinter import IntVar


difficulty = 0.40

def set_settings():
    print("\n\n\nYou selected the option " + str(var.get()))
    settings.destroy()

settings = tk.Tk()
settings.config(bg = "white")
settings.geometry("+600+400")
settings.protocol("WM_DELETE_WINDOW", set_settings)
settings.title('Settings')
settings.resizable(False, False)

MODES = [
            ("Child's play", 0.15),
            ("Very Easy", 0.30),
            ("Easy", 0.40),
            ("Normal", 0.50),
            ("Hard", 0.60),
            ("Very Hard", 0.70),
            ("Insanely Hard", 0.80),
            ("Impossible", 0.90)
        ]

label_1 = tk.Label(settings, font = 'Times 20', text = "Difficulty:", bg = "white")
label_1.grid(row = 0, column = 0, padx = 10)

frame_1 = tk.Frame(settings, bg="white")
frame_1.grid(row = 1, column=2, columnspan = 2)

var = IntVar()
var.set(difficulty)
for text, value in MODES:
    print("TEXT and VALUE", text, value)
    r_button = tk.Radiobutton(frame_1, font="Times 13", bg="white", text = text, variable=var, value=value)
    r_button.pack()

but_one = tk.Button(settings, font = 'Times 12', text = "OK", command = set_settings, bg = "white")
but_one.grid(row = 2, column = 2)

settings.mainloop()

0 个答案:

没有答案