Python Forum
Problem with If else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with If else statement
#1
I am building an application. And The following code works fine when Button first clicked. But next time button clicked, I get no change. I don't get an error, just get nothing,...the "On: does not change to "Off"

def set_drfvalue():
	on = 0
	if on == 0:

		drf_label = Label(factor_build,text= "On",  fg= "#581003", font = ("sans_serif  10 bold")).place(x =130, y=25)
		on = 1
	else:
		
		drf_label2 = Label(factor_build,text= "Off",  fg= "#581003", font = ("sans_serif  10 bold")).place(x =130, y=25)
		on = 0

def build_factors():
   my_button20 = Button(factor_build, text = "DRF Speed", width = 15, command = set_drfvalue) 
   my_button20.place(x=10, y=25)
I know I must be missing something. But followed a video to a T and no go.

Thank You
Reply
#2
What is line 2 there for? Because it is there, the else block will never be reached.

If on is supposed to be a global variable visible outside the function, you will need to declare it as a global.

If it's not supposed to be outside the function, you might want to create a closure. Then you don't have to initialize the variable every time it's called.
Reply
#3
Can you post the video Milfredo, maybe different eyes can spot something you did not code properly from the video. Line 2 does look odd.
Reply
#4
I see the problem. Yes each time the button gets clicked variable gets reset to 0. So question then is how do I initialize the variable?

Thanks
Reply
#5
The best thing to do is use a a control that displays state. If a button toggles something between zero and 1, the button should display the state of the thing it toggles. That way you don't need a separate display. A checkbox would be a good choice. This is a good user interface design thing and not a Python or tkinter thing.

To do what you want to do you need a persistent variable. In your function set_drfvalue() the variable "on" only exists while the function is running. Once set_drfvalue() is done, "on" is g"on"e. To make "on" persistent we we need to move "on" out of drfvalue.

This is the easiest solution, but it is really bad.
drfvalue = 0

def toggle_drfvalue():
    global drfvalue 
    if drfvalue  == 0:
        drf_label = Label(factor_build,text= "On",  fg= "#581003", font = ("sans_serif  10 bold")).place(x =130, y=25)
    else:      
        drf_label2 = Label(factor_build,text= "Off",  fg= "#581003", font = ("sans_serif  10 bold")).place(x =130, y=25)
    drfvalue = (drfvalue + 1) % 2
 
def build_factors():
   my_button20 = Button(factor_build, text = "DRF Speed", width = 15, command = toggle_drfvalue) 
   my_button20.place(x=10, y=25)
We made "on" a variable in the module namespace. Since "on" is no longer contained by a function it will persist after the function is done running. We had to rename "on" because it had a terrible name that means nothing when defined outside the scope of set_drfvalue(). We had to initialize "on" to a value. And whe have to tell toggle_drfvalue() that it should look in the module namespace for "on" (now drfvalue).

This solution works fine for 1 button and 1 variable. Things get ugly when you have a dozen controls and a dozen variables and unusable when you have hundreds of controls and hundreds of variables. I was shocked to discover the last system I worked on has over 1000 different controls and displays. It isn't even that big of a system. Using global would not work for something like that.

If you want to toggle something, use a checkbox.
Reply
#6
Thank you. Problem is I have coded 60 factor buttons. Previous version had checkboxes but they took up too much room. I have another 10 to 20 factors that must be accounted for in the near future. I will see if I can come up with a work around and maybe create a list and append every time a button is clicked.

Thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 1,650 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 2,419 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,587 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,761 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 2,424 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 4,964 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 2,518 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,192 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,260 May-20-2019, 09:26 AM
Last Post: avorane
  if statement and in operator problem bobger 5 3,998 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020