I’m trying to use GtkApplication
with GtkApplicationWindow
. The latter is defined in a UI file. I can’t figure out how to properly set things up.
This is my Glade UI template outline:
And this is the Python code so far:
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gio, Gtk
# NOTE: Not being used for this attempt.
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class Application(Gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id="org.opensource.mynotes",
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
**kwargs)
self.window = None
self.add_main_option("test", ord("t"), GLib.OptionFlags.NONE,
GLib.OptionArg.NONE, "Command line test", None)
def do_startup(self):
Gtk.Application.do_startup(self)
def do_activate(self):
if not self.window:
# self.window = AppWindow(application=self, title="My Notes")
builder = Gtk.Builder()
builder.add_from_file("./mynotes.ui")
app_window = builder.get_object("app_window")
app_window.show_all()
self.window = app_window
self.window.present()
def do_command_line(self, command_line):
options = command_line.get_options_dict()
# convert GVariantDict -> GVariant -> dict
options = options.end().unpack()
if "test" in options:
# This is printed on the main instance
print("Test argument recieved: %s" % options["test"])
self.activate()
return 0
def on_about(self, action, param):
about_dialog = Gtk.AboutDialog(transient_for=self.window, modal=True)
about_dialog.present()
def on_quit(self, action, param):
self.quit()
if __name__ == "__main__":
app = Application()
app.run(sys.argv)
How to properly use GtkApplicationWindow
from Glade instead of from Python code? In other words, how to start the Application making use of an UI file which already contains GtkApplicationWindow
? My current attempt causes the application open and immediately close. No errors on the terminal.
Hmm. Looks like adding app_window.set_application(self)
in do_activate
works. Still, eager to know of any other considerations.
I think I had a problem with that too some years ago, and google gave me a solution from stackoverflow. I think I manually edited the xml file. At least the xml from
High level GObject-Introspection based GTK3/GTK4 bindings for Nim language
worked. It starts with
<object id="window" class="GtkApplicationWindow">
<property name="visible">True</property>
I think the visible property was important too, without it I had to use window.showAll() in code.
Maybe I got it from this link:
gtk, glade
1 Like
baedert
(Timm Bäder)
October 14, 2019, 7:01am
4
You should never set a toplevel window to visible in the UI file. That will immediately show it, but you want to programmatically control when that happens.
Well – I think for plain example code or toy programs it may be better to set toplevel window to visible in the UI file. Otherwise users do not use showAll() and wonder why they don’t see the GUI. I got at least one issue about this:
opened 02:58AM - 12 Jan 19 UTC
I noticed that this repo was missing a glade example and I wanted to contribute … one; a little dice rolling app. I'm testing this on Linux right now.
Here is my glade file:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="diceRollWindow">
<property name="can_focus">False</property>
<property name="default_width">300</property>
<property name="default_height">100</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkRadioButton" id="d4Radio">
<property name="label" translatable="yes">D4</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="d6Radio">
<property name="label" translatable="yes">D6</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">d4Radio</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="d8Radio">
<property name="label" translatable="yes">D8</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">d4Radio</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="d10Radio">
<property name="label" translatable="yes">D10</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">d4Radio</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="d12Radio">
<property name="label" translatable="yes">D12</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">d4Radio</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="d20Radio">
<property name="label" translatable="yes">D20</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">d4Radio</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">5</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="resultLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="rollButton">
<property name="label" translatable="yes">Roll</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
```
Before adding in the functionality, I wanted to make sure that the window would display. I have this in python, and the window does show up:
```python
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file('DiceRoll.glade')
window = builder.get_object('diceRollWindow')
window.show_all()
Gtk.main()
```
With this Nim code, the program is exiting in an instant without showing the window at all. The terminal output `Display Window` does show up though.
```nim
import gintro/[gtk, glib, gobject, gio]
proc appActivate (app: Application) =
let builder = newBuilder()
let success = builder.addFromFile("DiceRoll.glade") > 0
if success:
let window = cast[Window](builder.getObject("diceRollWindow"))
window.showAll
echo("Display Window")
else:
echo("Error loading glade file")
proc main =
let app = newApplication("org.gtk.DiceRoll")
connect(app, "activate", appActivate)
discard app.run
main()
```
Glade is an essential part of the Gtk ecosystem. Is it a bug or did I forgot something?
Or users may ask: Why have I to call window.showAll() – my virtual basic app does not need that…
baedert
(Timm Bäder)
October 14, 2019, 9:05am
6
You are gonna run into problems if a GtkApplicationWindow
does not know about its application by the time it’s shown.
OK, then I will fix that in the Nim examples, thanks.
jrezai
October 15, 2019, 3:06am
8
It looks like your code is missing Gtk.main(), which starts the main loop for GTK.
Try adding it and see if it helps.
For example:
if __name__ == "__main__":
app = Application()
app.run(sys.argv)
# ------Start the main GTK loop-------
Gtk.main()
jrezai:
It looks like
Our code is fine, there is no gtk_main() needed when we use GtkApplication. See
system
(system)
Closed
October 29, 2019, 5:05am
10
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.