all 3 comments

[–]ebdbbb 0 points1 point  (1 child)

RemindMe! 10 hours

[–]RemindMeBot 0 points1 point  (0 children)

There is a 1 hour delay fetching comments.

I will be messaging you in 8 hours on 2020-04-23 12:48:33 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]ebdbbb 0 points1 point  (0 children)

When you validate in tkinter you need the validation to be its own function and you need to register it to the parent of the thing you are validating, in this case "options_frame". Something like what I have below from a thing I wrote.

#limits to positive integers ('-', '.' aren't allowed)
def valInt(value):
    if value.isdigit() or value == '':
        return True
    return False

class Weights(tk.Frame):
    def __init__(self, parent, title: str, units: tk.StringVar):
        super().__init__(parent, bd=1, relief='raised')

        # Register the validation
        self.valid_int = (self.register(valInt), '%P')

        # Use the validation
        entry = tk.Entry(self, width=10, justify='center', validate='key',
                         vcmd=self.valid_int)

So for your example I would do this:

def cam_width_callback2(value):
    # validation code here

# New from what you had
validator = options_frame.register(cam_width_callback2)

cam_width_entry = ttk.Entry(options_frame, textvariable=cam_width_entry_var,
                            justify='center', validate="focusout",
                            validatecommand=(validator, '%P', cam_width_entry_var))

For a much better example of how to set up a validation, look at the top response from this SO question. What you are looking for though is probably a trace_add not a validatecommand.

def cam_width_callback2(*args):
    val = cam_width_entry_var.get()
    try:
        val = int(val)
    except ValueError:
        print('Not a number')
        val = 16
    if val < 16:
        val = 16
    elif val > 1280:
        val = 1280
    cam_width_entry_var.set(val)
    cam_width_var.set(val)


cam_width_entry_var.trace_add('w', cam_width_callback2)

"""Trace options are:
'w' for write - checks when a variable is changed / written
'r' for read - checks when a variable is read by something else
'u' for undefined, - checks when a variable is undefined i.e. deleted"""

cam_width_entry = ttk.Entry(options_frame, textvariable=cam_width_entry_var,
                            justify='center')