I made a code that uses some functions. Such functions give a value to n and as they are called, the value of n changes (these functions are at the beginning of the code). What happens is that when you execute the script, so do not call the functions they all run and give a final value to n as if you 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)