GUI button proformance in pythonista

1.7k Views Asked by At

I am trying to make (what I thought) would be fairly simple GUI with a lighting control switch. I am using Pythonista on iOS. 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 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.

I'm very new to coding and just trying to learn and figure things out.

1

There are 1 best solutions below

0
On

Since I was testing without making a .pyui file (the file where you edit the actual GUI layout) I had to remove ui.loadview(). But this works and should give you a good starting point. The Pythonista UI Module can be tricky, but sticking with it is worth it :)


    import ui

    def lightControl(sender):
        if switch1.value == False:
            print 'Lights Off'
        elif switch1.value == True:
            print 'Lights On'

    switch1 = ui.Switch()
    switch1.action = lightControl
    v = ui.View()
    v.add_subview(switch1)

    v.present('sheet')

Also @Lukas is right, the Pythonista Forum is a great place to get help when you’re stuck.