我正在尝试使用glade创建警报日志,但它不起作用。我在这里做错了什么吗?
测试。空地:
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<object class="GtkButton" id="hello">
<property name="label" translatable="yes">hello</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_hello_clicked"/>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkDialog" id="dialog1">
<property name="border_width">5</property>
<property name="type_hint">normal</property>
<property name="has_separator">False</property>
<child internal-child="vbox">
<object class="GtkVBox" id="dialog-vbox1">
<property name="visible">True</property>
<property name="spacing">2</property>
<child>
<object class="GtkLayout" id="layout1">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="click">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="label" translatable="yes">process is not running</property>
</object>
<packing>
<property name="x">99</property>
<property name="y">58</property>
</packing>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child internal-child="action_area">
<object class="GtkHButtonBox" id="dialog-action_area1">
<property name="visible">True</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-yes</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_button1_clicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-no</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_button2_clicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="0">button1</action-widget>
<action-widget response="0">button2</action-widget>
</action-widgets>
</object>
</interface>
test.py
#!/usr/bin/python
import pygtk
import gtk
import gtk.glade
import os
class app:
def __init__( self ):
self.builder = gtk.Builder()
self.builder.add_from_file("test.glade")
self.window = self.builder.get_object('window1')
self.alert = self.builder.get_object('dialog1')
dic = {
"on_hello_clicked" : self.checkon,
}
self.builder.connect_signals( dic )
self.window.show_all()
def checkon(self, widget):
check_process = os.popen('pgrep firefox').read()
if check_process:
os.popen('pkill -9 firefox')
else:
self.alert.show()
addgui = app()
gtk.main()
我想到两件事(假设你的缩进是正确的,而不是像上面那样):
1)。不能像下面这样调用主循环:
addgui = app()
gtk.main()
2)。
有个错别字self.alert = self.builder.get_object('dailog1') ## dialog is spelled wrong