我是Gtk和Python的初学者。我正在使用Python 2.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()
这是林间文件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="<Enter Value> 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="<Enter Value> 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>
这是我处理按钮点击并同时调用下一个窗口的另一个文件:
from gi.repository import GObject, Gio, Gdk, Gtk
class lgn(object):
def __init__(self, application, builder):
self.Application = application
def loginbtn_clicked_cb(self, button):
builder = Gtk.Builder.new_from_file("blankwin.glade")
builder.connect_signals(self)
self.lgnwins = builder.get_object("applicationwindow1")
self.lgnwins.set_application(self.Application)
self.Application.get_window_by_id(2).set_visible(True)
self.Application.get_window_by_id(1).destroy()
这是第二个窗口的glade文件blankwin.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="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>
这个程序工作正常。但有几个胎儿缺陷。我的问题是:
当切换到另一个窗口时,UI显示第一个窗口关闭,第二个窗口打开,这对任何用户都是完全不可接受的。我不认为我正确地切换到其他窗口。在Gtk.Application方式中切换窗口的真实方法是什么?我无法在网络上找到正确的参考。找不到任何例子。
我还想回到我的第一个窗口,因为这个应用程序将有菜单栏,用户将在完成任务后返回主窗口。我怎么做?任何例子都会有所帮助。
有些标签不是英文的,不介意它们。谢谢你。
答案 0 :(得分:1)
这是.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>
我的版本的glade不支持GtkStack,所以这个例子有点hacky。希望你能弄清楚你需要什么。如果您不喜欢,我添加了一个可以禁用的转换。