2015-06-08 80 views
0

所以我有这样的代码:没有错误,但不会运行

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') 

     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') 

Injector() 

在IDLE控制台它工作正常,不寄托都我想要它做的。但是,只要我在我的桌面上运行.py文件。黑色窗口出现,然后关闭,没有任何反应。任何帮助?

+0

这不是问题所在。 – TigerhawkT3

+0

20秒后关闭,没有任何反应 –

回答

2

首先,你有两个名字相同的方法。第一个被第二个覆盖。在第二个结尾处,您需要以下行:

root.mainloop() 

这将实际运行GUI。从脚本运行时需要它,但在交互式解释器中运行时不需要。

在第二__openInjector的末尾添加它:

... 
self.code.config(yscrollcommand=scroll.set) 
frame.pack(fill='y') 
root.mainloop() 
+0

错误。你是否将该行添加到正确的功能? – TigerhawkT3

+0

我的评论/答案/测试简单,超过视觉和复杂性。我相应地删除了,因为这是正确的,(最重要的)简单的答案。 – BiTinerary

1

在你的第二个__openInjector方法的最后,添加一行:root.mainloop()

这是Tkinter运行您的代码所必需的。 mainloop实际上只不过是一个等待事件的无限循环。事件可以是用户交互,诸如点击按钮。

我的猜测是,当您为纯粹的便利原因交互式运行时,您不需要mainloop

相关问题