开发者

how to display dialog using pygtk with glade

I am trying create alert dailog using glade,but its not working .am i doing anything wrong here.

test.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>
            <proper开发者_StackOverflow中文版ty 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()


Two things off the top of my head (assuming your indentation is correct and not like it is above):

1.) You do not invoke the main loop like:

addgui = app()
gtk.main()

2.) You have a typo in

self.alert = self.builder.get_object('dailog1')  ## dialog is spelled wrong
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜