2

I am trying to build a GUI.

When I execute the application directly (i.e. double click the python file), I get a different result (console output) to importing it (mainloop).

I would like it to give the following console output:

c
d
e
f
g - from app

as I would like the main loop to be accessible after it has been imported as a module.

I am attempting to make the input and output controllable from an external file importing it as a module.

I get the expected output when I run the file, but when I import it as a module, it appears to run the main loop, as I get a Tkinter mainloop window output.

Here is the code:

class Application(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)
    
        self.text = Lines()
        self.text.insert("\n\n\n\n\n")
    
        self.waitingForInput = False
        self.inText = ""
    
        self.pack()
        self.widgets()

    def widgets(self):
        self.L1 = Label(self)
        self.L1["text"] = self.text.bottomLines
        self.L1.pack(side = "top")
    
        self.E1 = Entry(self)
        self.E1["width"] = 40
        self.E1.pack(side = "top")

        self.B1 = Button(self)
        self.B1["text"] = "Enter",
        self.B1["command"] = self.giveInput
        self.B1.pack(side = "top")

    def giveInput(self):
        if self.waitingForInput:
            self.inText = self.B1.get()
            self.waitingForInput = False

    def getInput(self, output):
        giveOutput(output)
    
        self.waitingForInput = True
        while True:
            time.sleep(0.1)
            if not self.waitingForInput:
                break
        return self.inText

    def giveOutput(self, output):
        self.text.insert(output)
        self.L1["text"] = self.text.bottomLines
        print self.text.bottomLines + " - from app"

root = Tk()
app = Application(master = root)
app.giveOutput("a \n b \n c \n d \n e \n f \n g")

The Lines class is essentially a stack of lines of text in a string, stacking more with insert(x) and accessing the final five lines of the stack with bottomLines.

Back on topic, when imported as a module, it runs the main loop, with a label containing what I assume to be 5 empty lines, an entry box and the "Enter" button. I do not want this. I want the same result as when I run the file directly, as I showed before.

I only want the box to appear when the app.mainloop method is called.

What have I done wrong, how is it wrong, and how can I correct it?

Community
  • 1
  • 1
Morgan
  • 33
  • 4

1 Answers1

3

This will only run when the module is run directly, not when imported:

if __name__ == "__main__":
    root = Tk()
    app = Application(master = root)
    app.giveOutput("a \n b \n c \n d \n e \n f \n g")
nbro
  • 15,395
  • 32
  • 113
  • 196
Isaac
  • 9,372
  • 4
  • 28
  • 31
  • Thanks! How did this cause the loop to start? – Morgan Nov 22 '14 at 20:55
  • When you import a module in python, the contents of that module are executed. The if statement here prevents the Application class from being instanciated only when imported from another module. which would execute the __init__ method, which in turn calls the getInput method where the while loop resides. – Isaac Nov 23 '14 at 00:54
  • Would this mean I would have to create an Application object from a separate file? – Morgan Nov 23 '14 at 10:54