Skip to main content
 首页 » 编程设计

python之使用 Tkinter 的单选按钮错误

2024年10月24日10飞鱼

我正在尝试使用 tkinter 获得一些单选按钮的基本 GUI,但是我在创建单选按钮时遇到了一些问题。

import Tkinter as tk   # python 
... 
def createView(self): 
        label = tk.Label(self, text="Choose mode analysis", font=TITLE_FONT) 
        label.pack(side="top", fill="x", pady=10) 
 
        form_analysis = tk.BooleanVar() 
 
        # form_analysis_radioButton = tk.RadioButton(self, text="Form Analysis") 
                                    # variable=form_analysis, value=True) 
        # match_analysis_radioButton = tk.RadioButton(self, text="Match Analysis", 
        #                                             variable=form_analysis, 
        #                                             value=False) 
        # form_analysis_radioButton.pack() 
        # match_analysis_radioButton.pack() 

抛出这个错误 createView 中的文件“gui_test.py”,第 72 行

    form_analysis_radioButton = tk.RadioButton(self, text="Form Analysis") 
AttributeError: 'module' object has no attribute 'RadioButton' 

这似乎是在告诉我 tk 模块中没有 RadioButton 函数(不知道为什么它说的是“module”而不是“tkinter”)所以我检查了命令行并得到了这个

In [2]: import Tkinter as tk 
 
In [3]: tk.RadioButton() 
--------------------------------------------------------------------------- 
AttributeError                            Traceback (most recent call last) 
<ipython-input-3-1404e954a1fa> in <module>() 
----> 1 tk.RadioButton() 
 
AttributeError: 'module' object has no attribute 'RadioButton' 
 
In [4]: from Tkinter import * 
 
In [5]: RadioButton() 
--------------------------------------------------------------------------- 
NameError                                 Traceback (most recent call last) 
<ipython-input-5-0d02b97652df> in <module>() 
----> 1 RadioButton() 
 
NameError: name 'RadioButton' is not defined 

有谁知道我做错了什么?提前感谢您的帮助。

请您参考如下方法:

这是一个简单的错字:RadioButton 应该替换为 Radiobutton .