首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tkinter无线电按钮IntVar属性错误

Tkinter无线电按钮IntVar属性错误
EN

Stack Overflow用户
提问于 2014-12-09 05:32:28
回答 1查看 5.3K关注 0票数 1

我知道,当使用无线电按钮时,如果要使用数字,则需要将变量设置为IntVar()。不幸的是,我下面运行的代码出现了以下错误:

代码语言:javascript
运行
复制
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'

有人知道为什么会这样吗?我想要做的是通过使用无线电按钮来改变加载到游戏中的图像。

代码语言:javascript
运行
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-09 05:36:18

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

首先调整代码以创建根小部件:

代码语言:javascript
运行
复制
root = Tk()  # <---
v = IntVar()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27372105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档