【问题标题】:Activating and Disabling button after process in python and pyGTK在 python 和 pyGTK 中处理后激活和禁用按钮
【发布时间】:2012-10-18 11:44:19
【问题描述】:

基本上,我想先让一个按钮“活动”,运行一个进程,然后在该进程完成运行后,再次禁用该按钮。

使用 pyGTK 和 Python,有问题的代码看起来像这样......

self.MEDIA_PLAYER_STOP_BUTTON.set_sensitive(True) #Set button to be "active"
playProcess = Popen("aplay " + str(pathToWAV) + " >/dev/null 2>&1",shell=True) #Run Process
playProcess.wait() #Wait for process to complete    
self.MEDIA_PLAYER_STOP_BUTTON.set_sensitive(False) #After process is complete, disable the button again

但是,这根本不起作用。

任何帮助将不胜感激。

【问题讨论】:

标签: python pygtk popen


【解决方案1】:

一切正常(python 2.7.3)。但是如果你在 gui 线程中调用 playProcess.wait() - 你会冻结 gui 线程而不重绘(对不起,我的英语不是很好)。你确定你尝试使用 subprocess.popen() 吗?也许是 os.popen()?

我的小测试:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk, gtk, gtk.glade
import subprocess

def aplay_func(btn):
        btn.set_sensitive(True)
        print "init"
        playProcess = subprocess.Popen("aplay tara.wav>/dev/null 2>&1", shell=True)
        print "aaa"
        playProcess.wait()
        print "bbb"
        btn.set_sensitive(False)

wTree = gtk.glade.XML("localize.glade")
window = wTree.get_widget("window1")
btn1 = wTree.get_widget("button1")
window.connect("delete_event", lambda wid, we: gtk.main_quit())
btn1.connect("clicked", aplay_func)
window.show_all()
gtk.main()

结果:

init
aaa
bbb

是的,按钮工作正常。声音也很好。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2021-07-30
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多