How Can I Execute More A Command Based On The Button That Called It? (classes And Tkinter)
Solution 1:
This method is highly problematic:
defconcat(self):
self = str(self)
numstring.join(self)
print(numstring)
You have not defined __str__
so str(self)
is going to be some funky string. Then numstring.join(self)
is a no-operation -- it returns a string you're not assigning to anything at all! So you're always printing the empty numstring
you started with.
And -- nowhere are you taking into account which key has been pressed... any keypress triggers exactly the same self.concat
call, no info left about "which key was it again?".
functools.partial
lets you bind arguments in advance (yes, you could do with a bedraggled lambda, but, you'll be a much happier camper if you forget about lambda
's existence...).
So for example one button should be...:
self.numpad_1 = tk.Button(num_frame, text='1',
command=functools.partial(self.concat, '1'))
and similarly for the others.
Now, the concat
method will receive the text corresponding to the button that was clicked and of course it needs to record it somewhere.
I'd recommend eschewing globals and instead starting the __init__
with
self.nums = []
Now concat
has an easy life:
def concat(self, digit):
self.nums.append(digit)
print(''.join(self.nums))
Incidentally, this lets you far more easily implement key features such as a delete key -- it just needs to drop the last item of the self.nums
list (self.nums.pop()
will suffice) and it's crucial to let the user correct a typo!
Solution 2:
You can use lambda
to make another function that call the original method with additional parameter
In addition to that, you need to declare global variable inside the method. To append a string to another string, you can use +=
operator.
classNumpad:
def__init__(self, master):
num_frame = tk.Frame(master)
num_frame.pack(side = tk.BOTTOM)
self.numpad_1 = tk.Button(num_frame, text="1", command=lambda: self.concat('1'))
self.numpad_1.grid(row=6, column=3)
self.numpad_2 = tk.Button(num_frame, text="2", command=lambda: self.concat('2'))
self.numpad_2.grid(row=6, column=4)
self.numpad_3 = tk.Button(num_frame, text="3", command=lambda: self.concat('3'))
self.numpad_3.grid(row=6, column=5)
self.numpad_4 = tk.Button(num_frame, text="4", command=lambda: self.concat('4'))
self.numpad_4.grid(row=5, column=3)
self.numpad_5 = tk.Button(num_frame, text="5", command=lambda: self.concat('5'))
self.numpad_5.grid(row=5, column=4)
self.numpad_6 = tk.Button(num_frame, text="6", command=lambda: self.concat('6'))
self.numpad_6.grid(row=5, column=5)
self.numpad_7 = tk.Button(num_frame, text="7", command=lambda: self.concat('7'))
self.numpad_7.grid(row=4, column=3)
self.numpad_8 = tk.Button(num_frame, text="8", command=lambda: self.concat('8'))
self.numpad_8.grid(row=4, column=4)
self.numpad_9 = tk.Button(num_frame, text="9", command=lambda: self.concat('9'))
self.numpad_9.grid(row=4, column=5)
self.numpad_0 = tk.Button(num_frame, text="0", command=lambda: self.concat('0'))
self.numpad_0.grid(row=7, column=4)
defconcat(self, n):
global numstring
numstring += n
print(n, numstring)
Post a Comment for "How Can I Execute More A Command Based On The Button That Called It? (classes And Tkinter)"