I made a code that uses some functions. Such functions give a value to n as they are called as the value of n changes (these functions are at the beginning of the code). What happens is that when executing the script, even if I don't call the functions, they are all executed and give it a final value, as if I had called them all and never called them. What happens?
my main application
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
self.nfont = tkfont.Font(family="Helvetica", size=15, slant="italic")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, Page1):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def p1():
global n
n = 1
print ("Probando")
My Page 1
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(bg="black")
# Declaramos y llamamos la tabla
x = 1
tabla = PhotoImage( file="tabla00.PNG")
labeltabla = Label(self, image=tabla)
labeltabla.image = tabla
labeltabla.pack()
label = tk.Label(self, bg="black", fg= "white", text="¿Tu número se encuentra en esa tabla?", font=controller.title_font)
label.pack()
si = tk.Button(self, text="Sí", command=SampleApp.p1(), width=10, height=2, relief="raised", borderwidth=5)
no = tk.Button(self, text="No", width=10, height=2, relief="raised", borderwidth=5)
pasar = tk.Button(self, text="Pasar a la siguiente página", width=20, height=3, relief="raised", borderwidth=5, command=lambda: controller.show_frame("Page2"))
si.pack()
no.pack()
pasar.pack()
My application start
class StartPage(tk.Frame):
def __init__(self, parent, controller):
# Definimos funciones básicas de la ventana
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(bg="black")
# Creamos y damos caracteristicas a los frames
titulo = tk.Label(self, bg="black", text="Bienvenidos...", fg= "white", font=controller.title_font)
instrucciones = tk.Label(self, bg="black", fg= "white", text="Instrucciones:", font=controller.title_font)
label = tk.Label(self, bg="black", fg= "white" , text="\nEste es un pequeño juego matemático que consiste en que tú piensas un número \nentre el 1 al 1023, y yo adivino cuál es.", font=controller.nfont)
label2 = tk.Label(self, bg="black", fg= "white", text="\nVas a pensar en un número antes de empezar. Al momento de que le des al botón, \nsaldrá una imagen con muuuchos números. No te asustes, los números están en orden. \nEs decir que si piensas en el 108, este debe estar entre el 107 y el 109 o lo que más se acerca.", font=controller.nfont)
# Empaquetamos los labels
titulo.grid(row=0)
label.grid(row=1)
instrucciones.grid(row=2, pady=20)
label2.grid(row=3, padx=30)
# Boton
button1 = tk.Button(self, text="Empezar",
command=lambda: controller.show_frame("Page1"), width=20, height=5, relief="raised", borderwidth=5)
button1.grid(row=4, pady=50)
In view of the code that the user puts as part of the question, which contains things like this:
It is evident that if the string appears when the program is launched
"Probando"
, it means that the functionp1()
has been executed at some point, even if it says that it has not.Since finally the user in a comment provides the full code in a link to this pastebin (and from which @JackNavaRow has subsequently very kindly added the relevant snippets to the question), I've been able to detect the point where those functions.
For example, as part of the code (which I do not reproduce in full because it is very long), this constructor appears in another class:
And there we can see the call, in the line that creates the button
si
:Your bug is that instead of providing the reference to the function to execute, which would be
SampleApp.p1
, you have called that function and provided the return value. It should have been like this:And like this, you have many other button creations that also call their function
command
instead of passing a reference to them.Clarification . To complete the analysis of the problem, the class that represents the entry point of the application, in its constructor, iterates through the rest of the classes and creates the frames instantiating all those classes.
Each one of those classes, when instantiated, executes its constructor, and within all those constructors a button is created
si
to which it tries to assign ascommand
each of the functionsSampleApp.p1
,SampleApp.p2
,SampleApp.p3
, etc... But in all cases it is committing the same error mentioned above, so those functions instead of being merely referenced, are executed.Therefore, when loading the main class, all those functions end up being executed through the constructors of the rest of the classes that are instantiated.