I need to use buttons to call one of two states inside a function, but I don't know what I'm doing wrong because the code is working wrong. The code does "print" because it's a simplified version of the code. How do I correctly make that with "Next" the result is 0, 1, 2... and with "Prev" it is 0, 5, 4, assuming I am moving in a 6 element set. As I have the "Next" function alone, I have the result "Next 1", "Next 2".... As I turn on the "Prev" method, I get "Next 1" "Prev 0",the next step "Next 1" "Prev 0" etc. at the same time. Thank you for your help :D.
import tkinter as tk
class Calc:
def __init__(self, root, get_save_analize=None, get_prev=None, get_next=None):
self.root = root
self.counter = 0
menu = tk.Menu(self.root)
file_menu = tk.Menu(menu)
self.root.config(menu=file_menu)
self.file_menu = menu
filemenu = tk.Menu(file_menu, tearoff=0)
helpmenu = tk.Menu(file_menu, tearoff=0)
file_menu.add_cascade(label="File", menu=filemenu)
# Image
file_menu.add_command(label="Previous", command=lambda: self.get_my_function(get_prev))
file_menu.add_command(label="Next", command=lambda: self.get_my_function(get_next))
def get_my_function(self, get_next=None, get_prev=None):
var = get_next
vap = get_prev
def get_next():
self.counter = 1
print('next', self.counter)
return self.counter
get_next()
def get_prev():
self.counter -= 1
print('prev', self.counter)
return self.counter
get_prev()
app = tk.Tk()
Calc(app).get_my_function()
app.mainloop()
CodePudding user response:
The problem that you are facing is that the is that currently everytime you call get_my_function
you end up calling both get_next
and get_prev
and your input variables get_prev
and get_next
are never used, nor are var
or vap
.
Still kind of guessing at what you are trying to do, but here how I got it to work for me if you want to keep as much of your code the same as possible and if you are indeed setting it up so that the get_next and get_prev functions get passed into Calc
import tkinter as tk
class Calc:
def __init__(self, root, get_save_analize=None, get_prev=None, get_next=None):
self.root = root
self.counter = 0
menu = tk.Menu(self.root)
file_menu = tk.Menu(menu)
self.root.config(menu=file_menu)
self.file_menu = menu
filemenu = tk.Menu(file_menu, tearoff=0)
helpmenu = tk.Menu(file_menu, tearoff=0)
file_menu.add_cascade(label="File", menu=filemenu)
# Image
file_menu.add_command(label="Previous", command=lambda: self.get_my_function(get_prev=get_prev))
file_menu.add_command(label="Next", command=lambda: self.get_my_function(get_next=get_next))
def get_my_function(self, get_next=None, get_prev=None):
var = get_next
vap = get_prev
print(f"get_my_func, {var=}, {vap=}")
if var is not None:
var() # this is the function get_next which was originally input when Calc was created
def get_next():
self.counter = 1
print('next', self.counter)
return self.counter
get_next()
if vap is not None:
vap() # this is the function get_prev which was originally input when Calc was created
def get_prev():
self.counter -= 1
print('prev', self.counter)
return self.counter
get_prev()
app = tk.Tk()
def input_get_next():
print("calling input_get_next (var)")
def input_get_prev():
print("calling input_get_prev (vap)")
c = Calc(app, get_next=input_get_next, get_prev=input_get_prev)
app.mainloop()