如何使用pygtk和glade显示对话框

发布于 2024-11-02 16:47:14 字数 5768 浏览 3 评论 0原文

我正在尝试使用空地创建警报对话,但它不起作用。我在这里做错了什么吗?

测试.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>

测试.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()

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>
            <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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-11-09 16:47:14

我想到了两件事(假设你的缩进是正确的,而不是像上面那样):

1.)你没有像这样调用主循环:

addgui = app()
gtk.main()

2.)你有一个拼写错误

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文