Button proformance in GUI
-
I am trying to make (what I thought) would be fairly simple GUI with a lighting control switch. It's not actually controlling a light. I just want something that prints to the console that the switch is on or off. I'm extremely new to coding and I just can't figure it out. When I run the code the GUI will appear and print the state of the switch (upon loading) but it will not print if the switch is used after that.
#Lighting Control Switch import ui def lightControl(mySwitch): toggle.switch1 = mySwitch switch1 = "Off" or "On" action_On = False print(not action_On) action_Off = (not action_On) print(action_On) if action_On == True: print("Lights On") if action_Off == True: print("Lights Off") v = ui.load_view() v.present('sheet')
I want a switch that indicates to the console that it has been used and whether it is currently on or off.
No errors are printing and the GUI comes up as it should. Just not the result I want.
Again, I'm very new to coding and just trying to learn and figure things out. Any help is appreciated, thanks!
-
@Phuket2 You could take out one more line... How?
-
btn_action = lambda sender: print(sender.value) ui.Switch(action=btn_action).present('sheet')
-
@Phuket2 thanks for the replies! I know I still have tons of reading/learning to do. I’ve been trying to set myself little goals to accomplish. I guess I was just making it more difficult than it is. And it works great with the little bit of code you suggested. Hopefully I’ll get this thing figured out...Thanks so much!
-
@xxfunxx , thats great. I was in a hurry when I did the post, after when I read it back I didn't like how I wrote it. This forum is full of helpful and respectful people. My comments were a little blunt, we as they should have been more encouraging. As I say, I was just in a hurry and didn't choose my words well.
Anyway, please keep posting. Many ppl here to help you help you out.
BTW, a lot of my posts have been all over the place in the past are often still are :) I am still learning so many things.
Anyway, have a great day and happy programming :)
-
@cvp , thanks for answering :). If I didn't the answer, it would have done my head in for the afternoon :). I dont think I would have got it it actually as I do not use lambda functions. But I think to get the extra line out, my mind would have gone there as it pretty bare bones. But with your help I was able to get it to a single line of code:
@ccc, thanks for keep proding. It's always fun.
ui.Switch(action=lambda sender: print(sender.value)).present('sheet')
-
@Phuket2 I'm like you, I never use lambda, except in some sorts, but I had read the post of @ccc and I just wanted to try. I always prefer to only perform a little task per line, I think it's easier to understand and easier to maintain if code has to be updated.
-
@cvp, while I like helping people here if I can. It can be difficult. You want to help, sometimes the best code(that you are capable of) is not the best way to help them. Often, i can write better concise code than I do when trying to help. But I consider concise code often more difficult to understand when you are starting rather than just some simple procedural code lines. It's tricky. You want to help without enforcing bad habits or less than optimal code to achieve a result. But sometimes i think its more useful
-
@Phuket2 I agree, completely
-
@xxfunxx said:
Hello. I wrote something a long time ago that might help you understand programming a bit better:
http://forum.maratis3d.com/viewtopic.php?id=868
and
http://forum.maratis3d.com/viewtopic.php?id=840
-
@TutorialDoctor thanks for the links I’m sure I will read them again a few more times. Pretty insightful. Thanks!