Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
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!
-
@xxfunxx , really you need to read a little bit of the docs for the ui. I did something below that is a lot more than you are looking for. But I really didn't know how to approach your question given the code you posted. BTW, i am not trying to make you feel uncomfortable. This is how you learn. The code below, is not necessarily great, i did it quickly. I am going to post it. But as I write this, I can think of a far easier demo I will post after this.
import ui class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.light_images = ['iob:ios7_lightbulb_32', 'iob:ios7_lightbulb_outline_32'] self.img = None self.switch = None self.make_view() def make_view(self): img = ui.ImageView(frame=(0, 0, 32, 32)) img.Image = ui.Image.named(self.light_images[0]) self.img = img self.add_subview(img) switch = ui.Switch(action=self.switch_action) switch.x = 100 self.switch = switch self.switch_action(switch) self.add_subview(switch) def switch_action(self, sender=None): if not sender.value: self.img.image = ui.Image.named(self.light_images[0]) else: self.img.image = ui.Image.named(self.light_images[1]) # just to show you can do it print('light on is {}'.format(sender.value)) if __name__ == '__main__': f = (0, 0, 300, 400) v = MyClass(frame=f) v.present(style='sheet', animated=False) ```python
-
@xxfunxx Sorry, it could have been as simple as -
import ui def btn_action(sender): print(sender.value) sw = ui.Switch(action=btn_action) sw.present('sheet')
-
@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')
-
-
@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!