Skip to main content
 首页 » 编程设计

python之'Tkinter' 未定义,但正在导入

2025年05月04日42fff_TT

所以就像我有这段代码一样,我正在编辑/修改它以使其工作,有什么帮助吗?

这是我的错误

Traceback (most recent call last): 
  File "C:\Users\Andrew\Desktop\Injector.py", line 42, in <module> 
    f = Injector() 
  File "C:\Users\Andrew\Desktop\Injector.py", line 18, in __init__ 
    self.__openInjector() 
  File "C:\Users\Andrew\Desktop\Injector.py", line 33, in __openInjector 
    Tkinter.Button(root, text='Inject!', command=self.runInjectorCode).pack() 
NameError: name 'Tkinter' is not defined 

这是我的代码:

try: 
    # for Python2 
    from Tkinter import * 
except ImportError: 
    # for Python3 
    from tkinter import * 
 
class Injector: 
 
    def __openInjector(self): 
        root = Tk() 
        root.geometry('600x400') 
        root.title('Toontown Rewritten Injector') 
        root.resizable(False, False) 
 
    def __init__(self): 
        self.code = '' 
        self.__openInjector() 
 
    def runInjectorCode(self): 
        exec(self.code.get(1.0, 'end'), globals()) 
 
    def __openInjector(self): 
        root = Tk() 
        root.geometry('600x400') 
        root.title('Toontown Rewritten Injector') 
        root.resizable(False, False) 
 
        frame = Frame(root) 
        self.code = Text(frame, width=70, height=20) 
        self.code.pack(side='left') 
 
        Tkinter.Button(root, text='Inject!', command=self.runInjectorCode).pack() 
 
        scroll = Scrollbar(frame) 
        scroll.pack(fill='y', side='right') 
        scroll.config(command=self.code.yview) 
 
        self.code.config(yscrollcommand=scroll.set) 
        frame.pack(fill='y') 
 
f = Injector() 
f.go() 

请帮助我不知道我做错了什么。我对这个 python 东西很陌生,我正在修改另一个根本不起作用的代码。但这必须以某种方式

请您参考如下方法:

Tkinter.Button(root, text='Inject!', command=self.runInjectorCode).pack() 

应该是

Button(root, text='Inject!', command=self.runInjectorCode).pack() 

当您键入 from Tkinter import * 这行时,这意味着您不再需要将它们称为 Tkinter.Thing(),而是您现在应该引用它们作为 Thing() 直接

See this快速阅读有关如何导入 python 模块的信息,因为它将在未来为您节省大量调试工作