关于python中开关的一些问题 [英] Some questions about switches in python

查看:64
本文介绍了关于python中开关的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 StackOverflow 的第一个问题,所以如果某些信息不存在或者我错过了一些重要的东西,请耐心等待我,但无论如何我会尽力:)

This is my first Question here at StackOverflow, so please be patient with me if some info isn't present or I missed something important, but anyways i'll do my best :)

最近开始用Python2.7写代码,所以不太擅长.在玩 PyGtk、PyGObject、Glade 等时,我发现了一些关于开关的特别之处(还没有尝试过任何其他小部件,所以我不知道它是否发生在其他地方.很可能不会,我希望......)

Recently I started to code in Python2.7, so I'm not very good at it. While playing with PyGtk, PyGObject, Glade, etc I found something particular about switches (Haven't tried with any other widget, so I don't know if it happens somewhere else. Most likely it doesn't, I hope...)

我使用 Glade 制作了一个非常基本的 GUI,带有一个窗口"和一个开关"

I made a very basic GUI with a single "window" plus a "switch" using Glade

我的目标是在用户尝试激活它之后停用开关,如果之前出现了一些异常,例如:

My objective was to deactivate switch after user tried to activate it if some exeption raised up before, something like:

  • 激活它 --> * 发现错误 --> * 停用它

我写了一些代码,过了一会儿,我注意到这段代码创建了一个类似循环的块,之后阻塞了 GUI 的窗口:

I made some code, and after a while, I noted that THIS piece of code created a loop-like block, blocking GUI's window afterwards:

builder = Gtk.Builder()
window1 = builder.get_object('window')
switchie = builder.get_object('switchie')

switchie.set_active(False)

def Hi(switch, active):
    print switchie.get_active()
    switchie.set_active(not switchie.get_active())


switchie.connect("""notify::active""", Hi)

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

如果我是对的,只要点击switchie",switchie.connect"就会将switchie"对象与Hi"函数链接起来.

If i'm right, "switchie.connect" links "switchie" object with "Hi" func whenever "switchie" gets clicked.

但是如果我执行此操作并尝试打开开关,GUI 就会挂断.我确实尝试通过脚本 & 执行此操作命令行并添加打印开关状态",导致无限循环(True & False)

But if I execute this and try to turn switch on, GUI hangs up. I did try to execute this via script & command-line and adding the "print switch state", resulting in an endless loop (True & False)

我尝试了我制作的许多其他函数,但它们都不能解决这个问题.事实上,这就是我做的所有其他函数的本质".

I tried with many other funcs I made, but neither of them could solve this issue. In fact, this is the "essence" of all the other funcs I made.

为什么会这样?

循环在哪里?

我在某些方面错了吗?

感谢帮助!

(如果您需要查看我其余的错误函数,请直接索要它们,但我认为它们不会有帮助...)

(If you need to see the rest of my faulty funcs, just ask for 'em, but I don't think they'll help...)

推荐答案

你想像这样连接开关:

switchie.connect("""activate""", Hi)

每次点击它只会被调用一次.你所做的是在信号改变后连接它,所以它一直在改变,永远赶不上.你也会想要改变

This will only get called once for every time it is clicked. What you were doing is hooking up to the signal after it changed, so it was constantly changing, and never catching up. You will also want to change

def Hi(switch, active):

def Hi(switch, active = None):

用于键盘支持.

这篇关于关于python中开关的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆