0

So I have this code:

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

In the IDLE console it works fine and does everthing I want it to do. But whenever I run the .py file on my Desktop. The black window appears, then just closes and nothing happens. Any help?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

1

First, you have two methods in your class with the same name. The first one gets overwritten by the second one. At the end of that second one, you need the following line:

root.mainloop()

This will actually run the GUI. It's needed when running from a script, but not when running within the interactive interpreter.

Add it at the end of the second __openInjector:

...
self.code.config(yscrollcommand=scroll.set)
frame.pack(fill='y')
root.mainloop()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Incorrect. Are you adding the line to the correct function? – TigerhawkT3 Jun 08 '15 at 19:12
  • Woops, simple over sight and over complication of my comment/answer/testing. I've deleted accordingly since this is the correct, (most importantly) simple answer. –  Jun 08 '15 at 19:18
1

At the end of your second __openInjector method, add the line: root.mainloop().

This is necessary for Tkinter to run your code. mainloop is really nothing more than an infinite loop that waits for events. An event may be a user interaction, such as clicking a button.

My guess is you don't need mainloop when running interactively for purely convenience reasons.

Mike Shoup
  • 356
  • 1
  • 5