最近我一直在这里问一些与此有关的问题(转到我的个人资料,查看我最近的两个问题)
我的目标是制作一个Gtk.Switch,如果在用户试图激活它时检测到一个exeption,它会“自动停用”。
我的最后两次尝试不成功,在尝试编码“自动停用”功能时导致无限循环。
经过一些研究,我发现了一些关于通知的有趣内容。
看来,当你用object.freeze_notify()
“冻结”它们时,Gtk“存储”从对象到队列的通知
既然我知道Gtk将通知事件存储到一个时间队列中,那么“清除”该队列会很好,所以我可以避免因尝试从事件处理程序内部停用一个开关而产生的无限循环。
编辑:
我想这样做:
我希望这在两种状态下工作(ON& OFF)
如果用户想要关闭开关(它是开启),并且程序检测到一个例外,请再次自动打开开关,就像没有任何事情发生一样。反之亦然
我正在使用Python2.7,Glade设计GUI,Gtk 3.4和Ubuntu 14.04.5作为我的操作系统
#!/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()
此代码仅适用于从OFF到ON。从ON更改为OFF时,它将无法使用
示例:
如果我执行此代码,并尝试激活开关而不首先输入文本,它将不会更改,但如果您输入文本(这没关系,只需输入一个字母,如果你想)它会允许你将开关从OFF移到ON。不幸的是,如果您想将开关从ON切换到OFF,即使您尝试不先输入文本也不会做任何特殊的事情
代码中有一些存根函数和变量。它只是一些测试代码,所以它真的无关紧要,我会在尝试将此代码包含在最终程序中时将其处理