简体   繁体   English

Tkinter 单选按钮总是给出 0 的值

[英]Tkinter radio buttons always gives values of 0

I'm trying to use radio buttons to define a variable to be used in the rest of my script.我正在尝试使用单选按钮来定义要在脚本的其余部分中使用的变量。

When I run this code, shift is always 0.当我运行此代码时,移位始终为 0。

I have copied and pasted code from online doing something similar, but the values are always 0.我从网上复制并粘贴了类似的代码,但值始终为 0。

I am using Spyder to run python3.我正在使用 Spyder 运行 python3。

Thanks.谢谢。

from tkinter import *
root = Tk()

def pixelShift():
        shift = var.get()
        print(shift)

var = IntVar()

Radiobutton(root,
            text        = '1 Pixel',
            variable    = var,
            value       = 1,
            command     = pixelShift
            ).pack(anchor = W)

Radiobutton(root,
            text        = '10 Pixels',
            variable    = var,
            value       = 10,
            command     = pixelShift
            ).pack(anchor = W)

Radiobutton(root,
            text        = '100 Pixel',
            variable    = var,
            value       = 100,
            command     = pixelShift
            ).pack(anchor = W)

root.mainloop()

You are creating a new Tk() instance by using您正在使用创建一个新的Tk()实例

var = IntVar()

You should use你应该使用

root = Toplevel()

or或者

var = IntVar(root)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM