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?