Python-How to call up a function from another module?
Posted
by
user2691540
on Stack Overflow
See other posts from Stack Overflow
or by user2691540
Published on 2014-06-09T08:57:07Z
Indexed on
2014/06/09
9:24 UTC
Read the original article
Hit count: 261
I am making a project where I need several modules which are imported into one main module to make a pizza ordering service. Upon finishing this (to a working standard) I decided to re-code it so that the whole order is completed with one tkinter window, now instead of using input(), I use tkinter.Entry() etc. etc.
To do this I had to use functions for each step so it is broken up nicely, e.g. the code asks if the user wants pickup or delivery, the user clicks a button which sets some variables and one of the buttons is then configured to say "continue" and the command leads to the next step of the pizza ordering process e.g. getting the name.
The problem I have is that when I get past the last function of the first module the configured button has a command to go to a function in the second module, but it says that the command is not defined??? I have tried my way around this but cannot import the configured button variable into the next module, and anything else I tried gave no result, it simply doesn't go to the next module after the first module is done.
I have made the main tkinter window in the main module and have it that it will mainloop after importing the other modules so shouldn't the function I want to call upon be defined? How can I get from one function to the next if the latter is in a seperate module? Is this possible or do I have to rethink my approach and if so how?
Ok then, have made some more code to show what my problem is, this isn't what I am actually using but it's a lot shorter and has the same issue:
this is the main module:
import tkinter
mainwindow = tkinter.Tk()
# here i set the window to a certain size etc.
import mod1
import mod2
mainwindow.mainloop()
this is mod1:
import tkinter
def button1():
label.destroy()
button1.destroy()
button2.config(text = "continue", command = func2)
def button2():
label.destroy()
button1.destroy()
button2.config(text = "continue", command = func2)
label = tkinter.Label(text = "example label")
button1 = tkinter.Button(text = "button1", command = button1)
button2 = tkinter.Button(text = "button2", command = button2)
label.pack()
button1.pack()
button2.pack()
this is mod2:
def func2():
button2.destroy()
print ("haha it works...")
I still get the problem that func2 is not defined? Thanks in advance
© Stack Overflow or respective owner