Tkinter 单选按钮 IntVar 属性错误 [英] Tkinter radiobutton IntVar Attribute error

查看:65
本文介绍了Tkinter 单选按钮 IntVar 属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当您使用单选按钮时,如果您想使用数字,则需要将变量设置为 IntVar().不幸的是,我在下面运行的代码带有以下错误:

I know that when you use radiobuttons you need to set the variable to IntVar() if you want to use a number. Unfortunately the code I am running below comes with the following error:

Traceback (most recent call last):
  File "F:/Analysis and Maths/PD diagnostic tool/Project files/PD Tool v2.py", line 49, in <module>
v = IntVar()
File "C:\Python34\lib\tkinter\__init__.py", line 354, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python34\lib\tkinter\__init__.py", line 236, in __init__
self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'

有谁知道为什么会这样?我想做的是使用单选按钮更改加载到 pygame 中的图像

Does anyone know why this would be the case? What I would like to do is change the image that is loaded into pygame by using the radiobuttons

v = IntVar()

class Window(Frame):
def __init__(self, master=None): # This defines the main window
     Frame.__init__(self, master)
     self.master = master
     self.init_window()

def init_window(self):
    global v
    self.master.title("PD Tool")
    self.pack(fill = BOTH, expand = 1) # This packs the frame
    showImg(self)
    import_button = ttk.Button(self, text = "import", command = lambda: filename()).place(x = 10, y = 5)
    start_pygame_button = ttk.Button(self, text = "Start PD tool", command = lambda:
    start_pygame_window()).place(x = 10, y = 35)
    RadioButton1 = ttk.Radiobutton(self, text = "One", variable = v, value = 1).place(x = 10, y=220)
    RadioButton2 = ttk.Radiobutton(self, text = "Two", variable = v, value = 2).place(x = 10, y=310)
    RadioButton3 = ttk.Radiobutton(self, text = "Three", variable = v, value = 3).place(x = 10, y=410)
    RadioButton4 = ttk.Radiobutton(self, text = "Four", variable = v, value = 4).place(x = 10, y=510)
    RadioButton5 = ttk.Radiobutton(self, text = "Five", variable = v, value = 5).place(x = 10, y=600)
    RadioButton6 = ttk.Radiobutton(self, text = "Six", variable = v, value = 6).place(x = 10, y=700)



def active_image():
    global file_path
    if v == 1:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\one.png"
        print(v)
    elif v == 2:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\wo.png"
        print(file_path)
    elif v == 3:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\hree.png"
    elif v == 4:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\our.png"
    elif v == 5:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\ive.png"
    elif v == 6:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\six.png"
    else:
        file_path = "F:\Analysis and Maths\PD diagnostic tool\Images\one.png"

    active_image_loaded = pygame.image.load(file_path)

    gameDisplay.blit(active_image_loaded,(0,0))

推荐答案

您需要在创建 IntVar 对象之前创建根小部件.

You need to create the root widget before creating the IntVar object.

先调整代码,创建一个根小部件:

Adjust the code to create a root widget first:

root = Tk()  # <---
v = IntVar()

这篇关于Tkinter 单选按钮 IntVar 属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆