Python 如何使用Pygobject Gtk+在Gtk.Application中从一个窗口转换到另一个窗口;3.

Python 如何使用Pygobject Gtk+在Gtk.Application中从一个窗口转换到另一个窗口;3.,python,gtk,gtk3,pygobject,Python,Gtk,Gtk3,Pygobject,我是Gtk和Python的初学者。我正在使用Python2.7.14和Pygobject(pygi-aio-3.24.1_rev1-setup)。我有一个工作程序,可以调用其他窗口,显示该窗口并关闭原始窗口。详情如下: 这是主要节目 #!/usr/bin/python # coding=utf8 from gi.repository import GObject, Gio, Gdk, Gtk import blnkwin1 class MyApplication(Gtk.Applicati

我是Gtk和Python的初学者。我正在使用Python2.7.14和Pygobject(pygi-aio-3.24.1_rev1-setup)。我有一个工作程序,可以调用其他窗口,显示该窗口并关闭原始窗口。详情如下: 这是主要节目

 #!/usr/bin/python
# coding=utf8

from gi.repository import GObject, Gio, Gdk, Gtk
import blnkwin1

class MyApplication(Gtk.Application):
    # Main initialization routine
    def __init__(self, application_id, flags):
        Gtk.Application.__init__(self, application_id=application_id, flags=flags)
        self.connect("activate", self.new_window)

    def new_window(self, *args):
        AppWindow(self)

class AppWindow(object):
    def __init__(self, application):
        self.Application = application

        # Read GUI from file and retrieve objects from Gtk.Builder
        try:
            GtkBuilder = Gtk.Builder.new_from_file("Lgnwin.glade")
            GtkBuilder.connect_signals(blnkwin.lgn(application,GtkBuilder))
        except GObject.GError:
            print("Error reading GUI file")
            raise

        # Fire up the main window
        self.MainWindow = GtkBuilder.get_object("LoginWindow")
        self.MainWindow.set_application(application)
        self.MainWindow.show()
        if GtkBuilder.get_application.get_window_by_id(2):
            GtkBuilder.get_application.get_window_by_id(2).destroy()

    def close(self, *args):
            self.MainWindow.destroy()

# Starter
def main():
    # Initialize GTK Application
    Application = MyApplication("com.b.example", Gio.ApplicationFlags.FLAGS_NONE)

    # Start GUI
    Application.run()

if __name__ == "__main__": main()
这是glade文件Lgnwin.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkApplicationWindow" id="LoginWindow">
    <property name="width_request">800</property>
    <property name="height_request">600</property>
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="icon">icon100.png</property>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkLabel" id="label1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="vexpand">True</property>
                <property name="label" translatable="yes">लॉगीन करा</property>
                <attributes>
                  <attribute name="font-desc" value="Mangal 16"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="label2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_top">5</property>
                <property name="margin_bottom">5</property>
                <property name="label" translatable="yes">युजरनेम</property>
                <attributes>
                  <attribute name="font-desc" value="&lt;Enter Value&gt; 14"/>
                  <attribute name="foreground" value="#201f4a4a8787"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="uname">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_top">2</property>
                <property name="margin_bottom">2</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="label3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_top">5</property>
                <property name="margin_bottom">5</property>
                <property name="label" translatable="yes">पासवर्ड</property>
                <attributes>
                  <attribute name="font-desc" value="&lt;Enter Value&gt; 14"/>
                  <attribute name="foreground" value="#201f4a4a8787"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">3</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="pswd">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_top">2</property>
                <property name="margin_bottom">2</property>
                <property name="visibility">False</property>
                <property name="invisible_char">●</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">4</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="loginbtn">
                <property name="label" translatable="yes">लॉगीन</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="margin_top">10</property>
                <signal name="clicked" handler="loginbtn_clicked_cb" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">5</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkApplicationWindow" id="applicationwindow1">
    <property name="can_focus">False</property>
    <property name="default_width">800</property>
    <property name="default_height">600</property>
    <child>
      <object class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">This is Just a blank Window</property>
      </object>
    </child>
  </object>
</interface>
这是第二个窗口blankwin.glade的glade文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkApplicationWindow" id="LoginWindow">
    <property name="width_request">800</property>
    <property name="height_request">600</property>
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="icon">icon100.png</property>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkLabel" id="label1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="vexpand">True</property>
                <property name="label" translatable="yes">लॉगीन करा</property>
                <attributes>
                  <attribute name="font-desc" value="Mangal 16"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="label2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_top">5</property>
                <property name="margin_bottom">5</property>
                <property name="label" translatable="yes">युजरनेम</property>
                <attributes>
                  <attribute name="font-desc" value="&lt;Enter Value&gt; 14"/>
                  <attribute name="foreground" value="#201f4a4a8787"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="uname">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_top">2</property>
                <property name="margin_bottom">2</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="label3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_top">5</property>
                <property name="margin_bottom">5</property>
                <property name="label" translatable="yes">पासवर्ड</property>
                <attributes>
                  <attribute name="font-desc" value="&lt;Enter Value&gt; 14"/>
                  <attribute name="foreground" value="#201f4a4a8787"/>
                </attributes>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">3</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="pswd">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_top">2</property>
                <property name="margin_bottom">2</property>
                <property name="visibility">False</property>
                <property name="invisible_char">●</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">4</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="loginbtn">
                <property name="label" translatable="yes">लॉगीन</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="margin_top">10</property>
                <signal name="clicked" handler="loginbtn_clicked_cb" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">5</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkApplicationWindow" id="applicationwindow1">
    <property name="can_focus">False</property>
    <property name="default_width">800</property>
    <property name="default_height">600</property>
    <child>
      <object class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">This is Just a blank Window</property>
      </object>
    </child>
  </object>
</interface>

假的
800
600
真的
假的
这只是一个空白窗口
这个程序运行正常。但有几个胎儿缺陷。我的问题是:


  • 当切换到另一个窗口时,UI显示第一个窗口关闭,第二个窗口打开,这是任何用户都无法接受的。我认为我没有正确地切换到其他窗口。在Gtk中切换窗口的真实方式是什么?应用程序操作方式?我在网上的任何地方都找不到正确的参考资料。找不到任何示例

  • 我还想回到我的第一个窗口,因为这个应用程序将有菜单栏,用户将在完成任务后返回主窗口。我该怎么做?任何例子都会有帮助

  • 有些标签不是英文的,请不要介意。提前谢谢。

    这是.py文件

    #!/usr/bin/env python3
    
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    import os, sys
    
    UI_FILE = "pygtk_stack.ui"
    
    
    class GUI:
        def __init__(self):
    
            self.builder = Gtk.Builder()
            self.builder.add_from_file(UI_FILE)
            self.builder.connect_signals(self)
    
            self.stack = Gtk.Stack()
            self.stack.set_transition_type(Gtk.StackTransitionType.CROSSFADE)
            self.view_one = self.builder.get_object('box1')
            self.view_two = self.builder.get_object('box2')
            self.stack.add_named(self.view_one, "view one" )
            self.stack.add_named(self.view_two, "view two" )
            window = self.builder.get_object('window')
            window.add(self.stack)
            window.show_all()
    
        def view_two_activated (self, meuitem):
            self.stack.set_visible_child(self.view_two)
    
        def view_one_clicked (self, button):
            self.stack.set_visible_child(self.view_one)
    
        def on_window_destroy(self, window):
            Gtk.main_quit()
    
    def main():
        app = GUI()
        Gtk.main()
    
    if __name__ == "__main__":
        sys.exit(main())
    
    以及pygtk_stack.ui文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Generated with glade 3.18.3 -->
    <interface>
      <requires lib="gtk+" version="3.12"/>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="menuitem3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_View</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkMenuItem" id="menuitem1">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">View 2</property>
                        <property name="use_underline">True</property>
                        <signal name="activate" handler="view_two_activated" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
      <object class="GtkBox" id="box2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">Go back</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="view_one_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Second view</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <object class="GtkWindow" id="window">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">window</property>
        <property name="default_width">500</property>
        <property name="default_height">400</property>
        <signal name="destroy" handler="on_window_destroy" swapped="no"/>
        <child>
          <placeholder/>
        </child>
      </object>
    </interface>
    
    
    真的
    假的
    垂直的
    真的
    假的
    真的
    假的
    _看法
    真的
    真的
    假的
    真的
    假的
    视图2
    真的
    假的
    真的
    0
    真的
    假的
    垂直的
    回去
    真的
    真的
    真的
    假的
    真的
    0
    真的
    假的
    第二视图
    假的
    真的
    1.
    真的
    假的
    窗口
    500
    400
    

    我的glade版本不支持GtkStack,所以这个例子有点粗糙。希望你能找到你需要的。我添加了一个转换,如果您不喜欢,可以禁用它。

    “第一个窗口关闭,第二个窗口打开,这是任何用户都无法接受的”——如果您破坏当前窗口并显示另一个窗口,听起来一切都按照您的要求进行。你需要解释一下你预期会发生什么。我正试图切换到下一个窗口。我想像其他任何普通应用程序一样转到下一个窗口。我的应用程序将有登录窗口,然后是主菜单窗口,我将把菜单栏放在上面。从那里,用户需要用不同的菜单打开不同的窗口,并在完成任务后返回主菜单。我的代码没有这样做,我找不到任何方法可以这样做。是否同时打开第一个和第二个窗口?还是一次只开一扇窗?我想一次开一扇窗。我的问题是我认为我没有使用Gtk.Application,因为它是打算使用的。必须有一个正确的方法来改变窗口。我不知道怎么做。这些教程都没有说明如何管理多个窗口以及如何在它们之间进行更改。任何资源或示例都将非常有用。这可能会成为一次冗长的对话。你为什么不在Gtk中看到我?这是我想要的,但它不使用Gtk.Application。我正在考虑将ApplicationWindow与Gtk.Application一起使用。使用Gtk.Application类可以做到这一点吗?您是否特别需要Gtk应用程序?不要怀疑你或其他什么,只要确定你需要GTK应用程序的哪一个功能?我希望有一个中心对象,可以根据需要添加和删除我的glade文件。我期待着MVC的到来。我读了这页-这听起来像Gtk。应用程序是这样做的。这就是我想要Gtk.Application的原因。@RohitS如果您希望使用MVC类型的应用程序,GtkApplication不会为您做任何事情(而是创建更多的工作)。请发布一个新问题,详细说明您已经告诉我的内容,链接到这里,我将尽力帮助您。