Skip to content

Commit 9728770

Browse files
Passing the smart value to the callback for smart widgets
1 parent 4b56217 commit 9728770

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

examples/smart_checkbutton.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import tkinter as tk
2+
import tk_tools
3+
4+
5+
# this callback doesn't necessarily have to take the 'value', but it is considered good practice
6+
def callback(value):
7+
print(value)
8+
9+
10+
if __name__ == '__main__':
11+
12+
root = tk.Tk()
13+
14+
scb = tk_tools.SmartCheckbutton(root)
15+
scb.grid()
16+
17+
scb.add_callback(callback)
18+
19+
root.mainloop()
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import tk_tools
33

44

5-
def callback():
6-
print(drop_down.get())
5+
# this callback doesn't necessarily have to take the 'value', but it is considered good practice
6+
def callback(value):
7+
print(value)
78

89

910
if __name__ == '__main__':

tk_tools/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.9.1'
1+
__version__ = '0.9.2'

tk_tools/widgets.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def add_callback(self, callback: callable):
2323
:return: None
2424
"""
2525
def internal_callback(*args):
26-
callback()
26+
try:
27+
callback()
28+
except TypeError:
29+
callback(self.get())
2730

2831
self.var.trace('w', internal_callback)
2932

@@ -159,12 +162,13 @@ def callback():
159162
:param options: any options that are valid for tkinter.Checkbutton
160163
"""
161164
def __init__(self, parent, callback: callable=None, **options):
165+
self._parent = parent
162166
self.var = tk.BooleanVar()
163-
tk.Checkbutton.__init__(parent, variable=self.var, **options)
167+
super().__init__(self._parent, variable=self.var, **options)
164168

165169
if callback is not None:
166170
def internal_callback(*args):
167-
callback()
171+
callback(self.get())
168172
self.var.trace('w', internal_callback)
169173

170174

0 commit comments

Comments
 (0)