Python Gtk+3 Window.present not work with GNOME Builder - python

environment:
Ubuntu Gnome 16.04.3
GNOME Builder 3.26.3
The GNOME Builder generated project use GtkTemplate to interpret ui, But I change it to use Gtk.Builder to interpret. After modifying, it does not work.
And I can not see any error message.
class Application(Gtk.Application):
def __init__(self):
super().__init__(application_id='org.gnome.Ee',
flags = Gio.ApplicationFlags.FLAGS_NONE)
def do_startup(self):
print('do_startup')
Gtk.Application.do_startup(self)
builder = Gtk.Builder()
builder.add_from_resource("/org/gnome/Ee/window.ui")
print(builder)
self.builder = builder
def do_activate(self):
print("do_activate")
win = self.props.active_window
if not win:
win = self.builder.get_object("EeWindow")
print(win)
win.present() # not work, can not see the window
I have upload the demo code to Github: https://github.com/Honghe/gnome_builder_demo
Any help will be appreciated!

The application does not know about the window so it exits immediately. You need to call win.set_application(self) after you created an instance of the window.

Related

How to enable hardware acceleration with python-vlc in Gtk3 application

Hello I am writing python application with gtk and vlc. Playback works, but without hardware acceleration.
import vlc
class Player:
def __init__(self):
self.instance = vlc.Instance("--no-xlib")
self.player = self.instance.media_player_new()
def visualize(self,widget):
self.player.set_xwindow(widget.get_window().get_xid())
def playurl(self,url):
media = self.instance.media_new(url)
self.player.set_media(media)
self.player.play()
When I run it I see:
[00007f4dfc014830] main filter error: Failed to create video converter
[00007f4df80af2a0] vdpau_avcodec generic error: Xlib is required for VDPAU
So I decided to remove "--no-xlib" parameter, but now I see this:
[00007f16b40078f0] egl_x11 gl error: Xlib not initialized for threads
Someone said this can help:
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
I've added this on start of my main function. But it causes segmentation fault when calling Gtk.main()
def main():
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
builder = Gtk.Builder()
builder.add_from_file(BUILDER_PATH)
mw = MainWin(builder) # class containing widgets loaded from GtkBuilder
mw.Show()
myvlc = Player()
myvlc.visualize(mw.player) # mw.player is GtkDrawingArea
myvlc.playurl("http://livesim.dashif.org/livesim/ato_10/testpic_2s/Manifest.mpd")
Gtk.main() # Crash
if __name__ == "__main__":
main()
PS: When I start vlc player /usr/bin/vlc HW acceleration works normally.

tkinter using default Mac theme

i am working on a tkinter app and I just updated to the latest version of python(python 3.9.5) and tkinter is using my mac's default theme, which doesn't go with my design.
i want this(the light theme):
but what am getting is(the dark theme):
is there a way I can change the theme to light or to check the current mac theme?
my code:
from tkinter import *
class Snipper:
def __init__(self):
self.root = Tk()
self.root.title("snipper")
self.build()
# bring app to front after start
self.root.lift()
self.root.attributes('-topmost',True)
self.root.after_idle(self.root.attributes,'-topmost',False)
self.root.minsize(1000, 600)
self.root.mainloop()
s = Snipper()

How to play custom GUI sounds in GTK3 Python app?

Tinkering with a small GTK3 app in Python3, I can trigger XDG theme sounds like so:
#!/usr/bin/python3
import gi
gi.require_version("Gtk", "3.0")
gi.require_version('GSound', '1.0')
from gi.repository import Gtk, GSound
class PyApp:
def __init__(self):
super(PyApp, self).__init__()
self.builder = Gtk.Builder()
self.builder.add_from_file("test.glade")
handlers = {
"onButtonClick": self.onButtonClick
}
self.builder.connect_signals(handlers)
self.sound = GSound.Context()
self.sound.init()
self.window = self.builder.get_object("window1")
self.window.show_all()
def onButtonClick(self, button):
self.sound.play_simple({ GSound.ATTR_EVENT_ID : "phone-incoming-call" })
if __name__ == "__main__":
app = PyApp()
Gtk.main()
But how do I make GSound play my own sounds? Do I have to construct a custom XDG theme, and if so do I have to install this in the correct system folder (e.g. /usr/share/sounds/), or can I bundle it stand-alone with my app? Also, how do I tell the Gsound.context() which theme to use? Very little information available about this.
Edit: This looked promising:
self.sound = GSound.Context()
self.sound.set_attributes({GSound.ATTR_CANBERRA_XDG_THEME_NAME: "freedesktop"})
Alas:
gi.repository.GLib.Error: gsound - error - quark: Invalid argument (-2)

Wayland and Kivy: how to set window position?

Following a previous StackOverflow answer I made additions to one of the basic Kivy tutorial apps to position the window at the top left corner of the screen. This works as expected under Xorg, but not under Wayland/xwayland.
How do you change the Kivy window position when running under xwayland?
Code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
class MyPaintWidget(Widget):
pass
class MyPaintApp(App):
def build(self):
# Works under Xorg, not under Wayland
Window.top = 0
Window.left = 0
return MyPaintWidget()
if __name__ == '__main__':
MyPaintApp().run()
Kivy does have Wayland support, but it may not be compiled-in. See setup.py. Also note that this will require SDL2 support to be compiled-in, as well:
# change both to "True"
c_options['use_wayland'] = False
c_options['use_sdl2'] = False

Open a PyGTK program but do not activate it

I have a PyGTK program which is hidden most of the time, but with a keypress it shall come up as a popup. Therefore I want the program not to be activated when its opened. I tried several options to to that, with no success:
self.window.show()
self.window.set_focus(None)
Activates the program, but sets no focus.
self.window.set_accept_focus(False)
self.window.show()
self.window.set_accept_focus(True)
With the last command, the window gets activated.
self.window.show()
self.window.unset_flags(gtk.HAS_FOCUS)
Does nothing...
Btw. I am using Ubuntu 9.10 (metacity)
Build the window but don't call show() on it until it is ready to be activated. Then use self.window.present().
EDIT:
If you never want the window to be activated, why not try a notification popup? You need libnotify for this. There are Python bindings. Here is an example: http://roscidus.com/desktop/node/336
In combination with a toolbar applet, this could do what you want -- i.e. the notification is raised when the user either clicks on the applet or presses the key combination.
I figured out how to do it. See the example below:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import gobject
class HelloWorld:
window=None
def hello(self, widget, data=None, data2=None):
HelloWorld.window.set_accept_focus(True)
HelloWorld.window.present()
def __init__(self):
HelloWorld.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.button = gtk.Entry(50)
self.button.connect("focus-in-event", self.hello, None)
HelloWorld.window.add(self.button)
self.button.show()
HelloWorld.window.set_accept_focus(False)
self.button.connect('button-press-event', self.hello)
HelloWorld.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
hello = HelloWorld()
hello.main()

Categories

Resources