![]() |
Problem with If else statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with If else statement (/thread-29364.html) |
Problem with If else statement - Milfredo - Aug-30-2020 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 RE: Problem with If else statement - bowlofred - Aug-30-2020 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. RE: Problem with If else statement - Pleiades - Aug-30-2020 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. RE: Problem with If else statement - Milfredo - Aug-30-2020 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 RE: Problem with If else statement - deanhystad - Aug-30-2020 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. RE: Problem with If else statement - Milfredo - Aug-30-2020 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. |