2

Mainly that's the question here. In a previous attempt I tried to accomplish this by making some funcs, activating and de-activating a switch, etc, but with no good results.

You can see my previous question here:

Previous question here on StackOverflow

I want to do this:

  • User runs program --> Tries to activate Switch --> Program detects exception --> Don't close entire program, just deactivate switch again as if user hadn't touched it

My previous code was able to accomplish only when the switch was "Off".

I want this to work in both states (ON & OFF)

If user wants to turn switch OFF (It Was ON), and program detects an exception, turn switch ON automatically again, like if nothing happend & vice versa

I'm using Python2.7, Glade to design GUI, Gtk 3.4 and Ubuntu 14.04.5 as my OS

In this new question I'm asking for some help solving this.I've done research and I've wrote code earlier, but MY code won't work at all. (You can see it in my previous Question)

I need someone to contribute with some new piece of fundamental code just to make this work, because I've tried all I know and just nothing.If you're asking yourself why I didn't provide any code, it's because I'm trying to force people to answer totally new pieces of code.

Why? Maybe because my previous code was kinda faulty, or something that maybe was blocking it. I'M NOT ASKING PEOPLE TO WRITE DOWN MY ENTIRE PROGRAM, in fact this isn't the entire program, it's just a tiny piece of it

Thanks anyways.

EDIT: Here's my actual code

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import os
import subprocess
import sys
import subprocess
import signal
import time
from subprocess import call
import threading
from multiprocessing import Process
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf

builder = Gtk.Builder()
#builder.add_from_file("""./Testing2.glade""")
builder.add_from_string("""

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.2"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="halign">baseline</property>
    <property name="valign">baseline</property>
    <property name="title" translatable="yes">Test</property>
    <property name="resizable">False</property>
    <property name="window_position">center</property>
    <property name="icon_name">applications-accessories</property>
    <property name="urgency_hint">True</property>
    <property name="has_resize_grip">False</property>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="halign">center</property>
        <property name="valign">center</property>
        <property name="margin_left">24</property>
        <property name="margin_right">24</property>
        <property name="margin_top">24</property>
        <property name="margin_bottom">24</property>
        <property name="orientation">vertical</property>
        <property name="spacing">16</property>
        <property name="homogeneous">True</property>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="primary_icon_name">applications-games</property>
            <property name="placeholder_text" translatable="yes">Texto</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkSwitch" id="switch1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>


""")

window1 = builder.get_object('window1')
textie = builder.get_object('entry1')
switchie =  builder.get_object('switch1')

def TurnOn(switch, active):

    print switchie.get_active()

    if yay == "":
        switchie.set_active(not switchie.get_active())

def TurnOff(switch, active):

    print switchie.get_active()

    if yay == "":
        switchie.set_active(not switchie.get_active())

def ParentTrue(switch,active):
    yay = textie.get_text().rstrip()
    if yay == "":
        def Hi():
            print switchie.get_active()
            switchie.handler_block(connecting)
            switchie.set_active(False)
            switchie.handler_unblock(connecting)
            return True
        Hi()

def ParentFalse(switch,active):
    yay = textie.get_text().rstrip()
    if yay == "":
        def Hi():
            print switchie.get_active()
            switchie.handler_block(connecting)
            switchie.set_active(True)
            switchie.handler_unblock(connecting)
            return True
        Hi()

switchie.set_active(True)

def qck():
    while True:
        os.environ["SomeVar"] = str(switchie.get_active())


p1 = threading.Thread(target=qck)

p1.start()

if not os.environ["SomeVar"]:
    connecting = switchie.connect('notify::active', ParentFalse)
if os.environ["SomeVar"]:
    connecting = switchie.connect('notify::active', ParentTrue)


window1.set_position(Gtk.WindowPosition.CENTER)
window1.connect("delete-event", Gtk.main_quit)
window1.show_all()

if __name__ == '__main__':
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    Gtk.main()

This code just works from OFF to ON. It won't work when you change from ON to OFF

Example:

If I execute this code, and try to activate switch WITHOUT entering text first, It won't change, BUT if you enter text (It doesn't matter, just enter a single letter if you want) it will allow you to move the switch from OFF to ON. Unfortunately It won't do anything special if you want to move switch from ON to OFF even if you try to do so without entering text first

Community
  • 1
  • 1
PythonNoob
  • 235
  • 1
  • 2
  • 12
  • Welcome to SO @PythonNoob, can you edit your question and provide what you have used so far? This question is too vague without sample code to troubleshoot. – ewitkows Aug 16 '16 at 02:08
  • All right then. I'll add some of my actual code – PythonNoob Aug 16 '16 at 02:17
  • Done. I added all of my actual code. You can copy/paste this in a script and execute it. Try it by yourself if you want. This is the most complicated version of it, wrapped in a lot of funcs and variables. I though that, doing it this way, I could confuse python (Or something like it) and accomplish my objective. If you want , I can provide the "pure" version... – PythonNoob Aug 16 '16 at 02:36

0 Answers0