I am a beginner in Gtk and Python both. I am using Python 2.7.14 and Pygobject(pygi-aio-3.24.1_rev1-setup). I have a working program that can call other window, show that window and close the original window. Which is as follows: This is the main program.
#!/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()
This is glade file 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>
This is another file in which I am handling button click and also calling next window:
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()
This is glade file for second window 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>
This program is working alright. But has several fetal flaws.My problem is:
When switching to another window UI is showing the first window closes and second window opens which is totally unacceptable to any user. I don't think I am switching to other window correctly. What is the authentic way to switch windows in Gtk.Application way of doing things? I could not find correct reference anywhere on web. Couldn't find any examples.
I also want to go back to my first window as this application will have menu bar and user will return after doing the task back to the main window. How do I do that? Any Examples would be helpful.
Some Labels are not in english don't mind them.Thanks in Advance.