Blank window when lanching about dialogue twice? - python

I have an application design done, without any useful stuff happening, but I have two windows. One is the main window, and one is the about dialogue. They are all on one .ui file from Glade.
When I open the About dialogue using Help>About (in my program), everything works. Closing it and opening it again using the same method results in a blank window.
Here is my code:
#! /usr/bin/env python3
from gi.repository import Gtk
class window():
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("./personalinfo.ui")
self.mainWindow = self.builder.get_object("mainWindow")
self.mainWindow.connect("destroy", self.on_mainWindow_destroy)
self.mainWindow.set_title("Persona")
self.mainWindowMainBoxButtonBoxCancel = self.builder.get_object("mainWindowMainBoxButtonBoxCancel")
self.mainWindowMainBoxButtonBoxCancel.connect("clicked", self.on_mainWindowMainBoxButtonBoxCancel_clicked)
self.mainWindowMainBoxButtonBoxOK = self.builder.get_object("mainWindowMainBoxButtonBoxOK")
self.mainWindowMainBoxButtonBoxOK.connect("clicked", self.on_mainWindowMainBoxButtonBoxOK_clicked)
self.mainWindowMainBoxGenderBoxCombo = self.builder.get_object("mainWindowMainBoxGenderBoxCombo")
self.mainWindowMainBoxGenderBoxCombo.set_active(0)
self.mainWindowMainBoxMenuFileMenuQuit = self.builder.get_object("mainWindowMainBoxMenuFileMenuQuit")
self.mainWindowMainBoxMenuFileMenuQuit.connect("activate", self.on_mainWindowMainBoxMenuFileMenuQuit_activate)
self.mainWindowMainBoxMenuHelpMenuAbout = self.builder.get_object("mainWindowMainBoxMenuHelpMenuAbout")
self.mainWindowMainBoxMenuHelpMenuAbout.connect("activate", self.on_mainWindowMainBoxMenuHelpMenuAbout_activate)
self.mainWindow.show_all()
def on_mainWindow_destroy(self, widget):
print("destroy: 'mainWindow'")
Gtk.main_quit()
def on_mainWindowMainBoxButtonBoxCancel_clicked(self, widget):
print("clicked: 'mainWindowMainBoxButtonBoxCancel'")
Gtk.main_quit()
def on_mainWindowMainBoxButtonBoxOK_clicked(self, widget):
print("clicked: 'mainWindowMainBoxButtonBoxOK'")
Gtk.main_quit()
def on_mainWindowMainBoxMenuFileMenuQuit_activate(self, widget):
print("activate: 'mainWindowMainBoxMenuFileMenuQuit'")
Gtk.main_quit()
def on_mainWindowMainBoxMenuHelpMenuAbout_activate(self, widget):
print("activate: 'mainWindowMainBoxMenuHelpMenuAbout'")
self.aboutWindow = self.builder.get_object("aboutWindow")
self.aboutWindow.set_title("About Persona")
self.aboutWindowMainBoxButtonBoxOK = self.builder.get_object("aboutWindowMainBoxButtonBoxOK")
self.aboutWindowMainBoxButtonBoxOK.connect("clicked", self.on_aboutWindowMainBoxButtonBoxOK_clicked)
self.aboutWindow.show_all()
def on_aboutWindowMainBoxButtonBoxOK_clicked(self, widget):
print("clicked: 'aboutWindowMainBoxButtonBoxOK'")
self.aboutWindow.destroy()
w = window()
Gtk.main()

The About window isn't created each time you call self.aboutWindow = self.builder.get_object("aboutWindow") it is created once when you load the UI file. So when you call self.aboutWindow.destroy() it destroys it forever, and the next time you try to display the window it no longer exists.
Instead of destroying the window, you probably want to hide it instead self.aboutWindow.hide()

Related

How to restore focus on a widget after hidding then showing the parent widget?

I'm creating a launcher application (like Spotlight/Albert/Gnome-Do). I'm using Python 2.7 and Pyside. Made and used on Windows 10.
It is running in the background and listening to a shortcut with the keyboard (pip install keyboard). When the shortcut is called, a QObject signal calls the show method of my main widget.
My issue is that when the main widget gets hidden by pressing escape or return, next time the widget is shown, the focus will be in the QlineEdit and the user will be able to type its query straight away.
But when the widget is hidden by clicking outside widget (handled by filtering the QEvent WindowDeactivate), the focus won't be on my QLineEdit at next call, which ruins the user experience.
I've tried playing with activateWindow() or raise_(), but it doesn't change anything.
Heree here a simplified example code that shows my problem:
import sys
import keyboard
from PySide.QtCore import *
from PySide.QtGui import *
SHORTCUT = 'Ctrl+space'
class ShortcutThread(QObject):
signal = Signal()
class Launcher(QMainWindow):
def __init__(self, parent=None):
super(Launcher, self).__init__()
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Popup)
self.resize(500, 50)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout_ = QHBoxLayout()
self.central_widget.setLayout(self.layout_)
self.search = QLineEdit()
self.layout_.addWidget(self.search)
def eventFilter(self, obj, event):
# Hide dialog when losing focus
if event.type() == QEvent.WindowDeactivate:
self.hide()
return super(Launcher, self).eventFilter(obj, event)
def keyPressEvent(self, e):
# Hide dialog when pressing escape or return
if e.key() in [Qt.Key_Escape, Qt.Key_Return]:
self.hide()
def main():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
launcher = Launcher()
shortcut = ShortcutThread()
shortcut.signal.connect(launcher.show)
keyboard.add_hotkey(SHORTCUT, shortcut.signal.emit, args=[])
launcher.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
When I call the shortcut (Ctrl+Space here) and click elsewhere, next time I'll call the shortcut, the focus won't be set to the QLineEdit widget.
When the launcher is hidden by hitting return or escape, it does work as expected.

Issues when attaching and detaching external app from QDockWidget

Consider this little piece of code:
import subprocess
import win32gui
import win32con
import time
import sys
from PyQt5.Qt import * # noqa
class Mcve(QMainWindow):
def __init__(self, path_exe):
super().__init__()
menu = self.menuBar()
attach_action = QAction('Attach', self)
attach_action.triggered.connect(self.attach)
menu.addAction(attach_action)
detach_action = QAction('Detach', self)
detach_action.triggered.connect(self.detach)
menu.addAction(detach_action)
self.dock = QDockWidget("Attach window", self)
self.addDockWidget(Qt.RightDockWidgetArea, self.dock)
p = subprocess.Popen(path_exe)
time.sleep(0.5) # Give enough time so FindWindowEx won't return 0
self.hwnd = win32gui.FindWindowEx(0, 0, "CalcFrame", None)
if self.hwnd == 0:
raise Exception("Process not found")
def detach(self):
try:
self._window.setParent(None)
# win32gui.SetWindowLong(self.hwnd, win32con.GWL_EXSTYLE, self._style)
self._window.show()
self.dock.setWidget(None)
self._widget = None
self._window = None
except Exception as e:
import traceback
traceback.print_exc()
def attach(self):
# self._style = win32gui.GetWindowLong(self.hwnd, win32con.GWL_EXSTYLE)
self._window = QWindow.fromWinId(self.hwnd)
self._widget = self.createWindowContainer(self._window)
self.dock.setWidget(self._widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Mcve("C:\\Windows\\system32\\calc.exe")
w.show()
sys.exit(app.exec_())
The goal here is to fix the code so the window attaching/detaching into a QDockWidget will be made properly. Right now, the code has 2 important issues.
Issue1
Style of the original window is screwed up:
a) Before attaching (the calculator has a menu bar)
b) When attached (the calculator menu bar is gone)
c) When detached (the menu bar hasn't been restored properly)
I've already tried using flags/setFlags qt functions or getWindowLong/setWindowLong but I haven't had luck with all my attempts
Issue2
If you have attached and detached the calculator to the mainwindow, and then you decide to close the mainwindow, you definitely want everything (pyqt process) to be closed and cleaned properly. Right now, that won't be the case, why?
In fact, when you've attached/detached the calculator to the mainwindow, the python process will hold and you'll need to force the termination of the process manually (i.e. ctrl+break conemu, ctrl+c cmd prompt)... which indicates the code is not doing things correctly when parenting/deparenting
Additional notes:
http://doc.qt.io/qt-5/qwindow.html#fromWinId
http://doc.qt.io/qt-5/qwidget.html#createWindowContainer
In the above minimal code I'm spawning calc.exe as a child process but you can assume calc.exe is an existing non-child process spawned by let's say explorer.exe
I found part of the issue wrt to closing. So when you are creating the self._window in the attach function and you close the MainWindow, that other window (thread) is sitting around still. So if you add a self._window = None in the __init__ function and add a __del__ function as below, that part is fixed. Still not sure about the lack of menu. I'd also recommend holding onto the subprocess handle with self.__p instead of just letting that go. Include that in the __del__ as well.
def __del__(self):
self.__p.terminate()
if self._window:
print('terminating window')
self._window.close
Probably better yet would be to include a closeEvent
def closeEvent(self, event):
print('Closing time')
self.__p.terminate()
if self._window is not None:
print('terminating window')
self._window.close

Proper way to open multiple appwindows with Python GTK3 Gtk.Application

I'm learning GTK3 under Python3 and made an app that has only one AppWindow so far.
I'm using Gtk.Application.
I can't figure out how to proper handle opening a second window.
I can open it directly from my main Window but I don't know how to pass Application object to it (I googled and "duckduckgoed" without any success).
Do I need to call Gtk.Application to open second window?
How do I let Gtk.Application track this new window?
Application is like this:
Main window with an objects list from a database.
Second window to edit a single item selected from the main window's
list.
Thanks.
My code (I stripped out the unnecesary code):
# ################################################################
# MAIN APP WINDOW
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Here all the widgets and a button to open the second window
self.show_all()
# ################################################################
# MAIN APP CLASS
class Application(Gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id=MIKUNA_ID,
**kwargs)
self.window = None
def do_startup(self):
Gtk.Application.do_startup(self)
action = Gio.SimpleAction.new("quit", None)
action.connect("activate", self.on_quit)
self.add_action(action)
def do_activate(self):
# We only allow a single window and raise any existing ones
if not self.window:
# Windows are associated with the application
# when the last one is closed the application shuts down
self.window = AppWindow(application=self, title="Mikuna")
self.window.present()
def on_quit(self, action, param):
self.quit()
if __name__ == "__main__":
app = Application()
app.run(sys.argv)
Second window to edit a single item selected from the main window's list.
You just create the second window from your main window then, probably a Gtk.Dialog that is transient to the main window. You only need to make the Application track it if it is a toplevel window you expect to out-live your main window.

PyGI window not destroying

Here is my class that is representing preferencies. It loads glade layout from 'preferences.glade'. btn_cancel_clicked_cb and btn_ok_clicked_cb are called when the corresponding buttons are activated. But self.destroy() doesn't do anything. Can somebody explain how to destroy this dialog when after clicking the buttons?
from gi.repository import Gtk
from common import Prefs
class ViewPrefs(Gtk.Dialog):
def __init__(self):
Gtk.Dialog.__init__(self)
self.builder = Gtk.Builder()
self.builder.add_from_file("preferences.glade")
self.builder.connect_signals(self)
self.rb_input=self.builder.get_object("rb_input")
self.rb_select=self.builder.get_object("rb_select")
def run(self, *args):
window = self.builder.get_object("window_prefs")
window.show()
window.connect('destroy', Gtk.main_quit)
Gtk.main()
def register_observer(self, controller):
self.controller = controller
def btn_cancel_clicked_cb(self,widget):
self.destroy()
def btn_ok_clicked_cb(self,widget):
active = [r for r in self.rb_input.get_group() if r.get_active()][0]
input_type=active.get_label().lower()
self.controller.set_prefs(Prefs(input_type=input_type))
self.destroy()
It starts from the main window like that:
prefsview=ViewPrefs()
prefsview.register_observer(self.controller)
prefsview.run()
self is not self.window. In fact, subclassing Gtk.Dialog for your case is useless as the dialog part of self is never used! If you are requiring a new enough version of GTK+, you can create your dialog as a composite widget template and build your class that way (I don't know how to do this with Python; sorry). Otherwise, get rid of the subclass and call window.destroy() instead (and, if window is really a Gtk.Dialog, window.run() in your self.run()).

Python Webkit-Browser: Problems on attaching the developer-toolbar

I want to dive in Python by building a simple browser-application. I've mad a minimalistic webkitbrowser with a tutorial and now want to extend the program, but I'm stuck at some tiny problems I cannot solve.
Python 3.3.3
using Glade for the UI
The first step is to simply add a second scrolledWindow in which the developer-tools should load, immediately.
Here is my .ui-file so far, and this is the python-code:
from gi.repository import Gtk, WebKit
UI_FILE = "browser.ui"
class Browser:
"""A simple Webkit-Browser in GTK+"""
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file(UI_FILE)
self.builder.connect_signals(self)
self.back = self.builder.get_object("back")
self.forward = self.builder.get_object("forward")
self.adress = self.builder.get_object("adress")
self.webview = WebKit.WebView()
scrolled_window = self.builder.get_object("scrolledwindow")
scrolled_window.add(self.webview)
self.settings = WebKit.WebSettings()
self.settings.set_property('enable-developer-extras', True)
self.webview.set_settings(self.settings)
self.devtools = WebKit.WebInspector()
scrolled_window_dev = self.builder.get_object("scrolledwindowDev")
scrolled_window_dev.add(self.devtools)
^^^^^
self.webview.connect("title-changed", self.on_title_changed)
self.window = self.builder.get_object("window")
self.window.show_all()
def on_title_changed(self, webview, frame, title):
self.window.set_title(title)
def on_button_clicked(self, button):
if button.get_stock_id() == Gtk.STOCK_GO_FORWARD:
self.webview.go_forward()
elif button.get_stock_id() == Gtk.STOCK_GO_BACK:
self.webview.go_back()
def on_entry_activate(self, widget):
url = widget.get_text()
if not "http://" in url:
url = "http://"+url
self.webview.load_uri(url)
def destroy(self, window):
Gtk.main_quit()
def main():
app = Browser()
Gtk.main()
if __name__ == "__main__":
main()
I get the error
TypeError: argument widget: Expected Gtk.Widget, but got
gi.repository.WebKit.WebInspector
Okay, this is stated in the reference of Webkit, that WebInspector is a GObject and not a GtkWidget. But I don't know what to do now.
So, can I make a GtkWidget from a GObject (if yes - how) or should I attach the dev-tools in a complete different way?
The inspector, as you noted, isn't a widget. It's a web page, so you need to create another webview for it. You do this by getting self.window.props.web_inspector (don't create a new inspector) and connecting to its inspect-web-view signal. Inside that signal handler, you need to create a new webview, add that webview to a window or wherever you want to display it, and return it.
You'll probably also want to handle the show-window, attach-window, detach-window, and close-window signals.
More documentation here: inspect-web-view
Example of running Inspector in separate window. Webkit-gtk.
This gist without many signals connected.
https://gist.github.com/alex-eri/53518825b2a8a50dd1695c69ee5058cc

Categories

Resources