khushi
khushi

Reputation: 365

No error message shows up even if no Radiobutton is selected - what's wrong with my code?

So I have two functions:

def Driver_Form():
    canvas.delete("all")
    root.configure(bg="cornflower blue")
    canvas.config(width=root.winfo_screenwidth(), height=root.winfo_screenheight(),
                  bg="sky blue") 
...

myvar1 = StringVar() #IntVar()
Male = Radiobutton(canvas, text = "Male", variable = myvar1, value = 1)
canvas.create_window(734, 265, window = Male, height = 25, width = 70)

Female = Radiobutton(canvas, text="Female", variable=myvar1, value = 2)
canvas.create_window(825, 265, window=Female, height=25, width=90)

Other = Radiobutton(canvas, text="Rather not say", variable=myvar1, value = 3)
canvas.create_window(940, 265, window=Other, height=25, width=120)

Next_button = Button(root, text="Next", anchor=CENTER, command=lambda: check_Driver(FName_Entry, LName_Entry,
                                                                                        day_combobox, month_combobox,
                                                                                        year_combobox, myvar1, Email_Entry, Phone_Entry))
    Next_button.configure(width=10, bg="black", fg="blue", border=10)
    canvas.create_window(920, 450, anchor=NW, window=Next_button)

the Next_button leads to another function:

def check_Driver(FName_Entry, LName_Entry, day_combobox, month_combobox, year_combobox,  myvar1, Email_Entry,
                 Phone_Entry, event=None):
...

if myvar1 != 1 or 2 or 3:
    error_blank2 = Message(root, text="Please don't leave any field/s blank", anchor=CENTER, bg="red", width=1000)
    canvas.create_window(600, 450, anchor=NW, window=error_blank2)

I am trying to make sure that an error message shows up if none of the radio buttons are clicked out of male, female and other. However, I dont know what is wrong but it doesn't seem to work and no error message shows up even if none of the Radiobuttons are clicked.

Upvotes: 0

Views: 122

Answers (2)

Martin Wettstein
Martin Wettstein

Reputation: 2894

Your condition is wrong. The condition you wrote does not check whether the value is 1, 2, or 3, but it checks whether it is not 1 or whether 2 and 3 are numbers different from 0.

To check whether myvar is something else than 1, 2, or 3, use:

if myvar1.get() not in [1, 2, 3]:

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385980

You have at least two big problems. First, myvar1 will never be 1, 2, or 3 because myvar1 isn't an integer. It's an instance of StringVar. So, the first thing you need to change is that you need to use int(myvar1.get()) in your if statements so that you're comparing the value stored in the variable rather than the instance of StringVar itself.

When you fix that, you'll have another problem. if myvar1 != 1 or 2 or 3 is not doing what you think it does. It is logically the same as if (myvar != 1) or (bool(2)) or (bool(3)). Since bool(2) will always return True, the body of the if statement will always run.

If you want to check that myvar1 isn't one of the values 1, 2, or 3, you can do it one of these ways:

if int(myvar1.get()) not in (1, 2, 3):

-or-

value = int(myvar1.get())
if value != 1 and value != 2 and value != 3:

Upvotes: 1

Related Questions