i want to set blocked the window behind dialog to prevent the user do click or modify any content of the window while the dialog are running, and when the user close de dialog then set unlocked the window behind dialog.
import gtk;
window = gtk.Window();
window.set_title("Window Behind Dialog");
window.set_default_size(426,240);
textentry = gtk.TextView();
window.add(textentry);
window.show_all();
dialog = gtk.Window();
dialog.set_title("Dialog");
dialog.set_default_size(256,144);
label = gtk.Label("Unlock the window behind when this dialog get close");
dialog.add(label);
dialog.show_all();
gtk.main();
Which method is used for it, in Gtk or PyGtk?, for example:
window.set_disabled_to_all_events();
or
window.set_disabled();
or
window.events_disabled(True);
or
window.set_blocked(True);
If you have a window manager that honors modal windows, you could use set_modal on the dialog window.
If not, you could use set_sensitive on the parent window. Call this with False when the dialog is shown, and with True when the dialog is hidden or destroyed.
I've added Gtk3 examples below. I recommend that you switch to PyGObject and Python 3 before investing too much effort in a deprecated toolkit.
Modal window example:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.connect("destroy", Gtk.main_quit)
window.add(Gtk.TextView())
window.show_all()
dialog = Gtk.Window(title="Dialog")
dialog.set_transient_for(window)
dialog.set_modal(True)
dialog.show()
Gtk.main()
Or using explicit set_sensitive:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.connect("destroy", Gtk.main_quit)
window.add(Gtk.TextView())
window.show_all()
dialog = Gtk.Window(title="Dialog")
dialog.set_transient_for(window)
window.set_sensitive(False)
def destroy_cb(widget, data):
data.set_sensitive(True)
dialog.connect("destroy", destroy_cb, window)
dialog.show()
Gtk.main()
Related
So I've run into an issue where whenever I set the WindowTypeHint to anything other than normal the window just disappears. I've verified the hint type with print.Below is my sample code
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
# Create the main window
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Hello World")
# Innitially setting the bar to off
self.set_decorated(False)
# Attempting to set window type to prevent minimizing when show desktop is hit and to stay behind other objects etc.
self.set_type_hint(Gdk.WindowTypeHint.DOCK)
# Defining the button and drawing it
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
# define the button functions
def on_button_clicked(self, widget):
if win.props.decorated == False:
win.set_decorated(True)
else:
win.set_decorated(False)
print("Hello World")
print(win.props.decorated)
print(self.props.type_hint)
# Set alias for the window
win = MyWindow()
# Testing. Print the hint type to console
print(win.props.type_hint)
# Window settings.
win.set_keep_below(True)
win.connect("destroy", Gtk.main_quit)
win.show_all()
print(win.props.type_hint)
Gtk.main()
Window to appear as a dock on the desktop where the mouse pointer is or in the corner of one of the monitors the same way glava works with it's settings.
Item was drawing off screen. Multiple monitors had it throwing the item into the corner off screen between them. If anyone else comes across this I resolved it by setting the coordinates with self.move(x, y) in the initialisation. Found where I wanted it to root with win.get_position() on button press to find where I wanted it.
Gtk-WARNING **: 19:18:52.313: Can't set a parent on a toplevel widget
Why do I get this warning? Isn't master win a toplevel widget? What am I doing wrong?
Shouldn't master_win be toplevel? It is created first...
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
#some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
master_win.add(win)
win.connect("destroy", Gtk.main_quit)
master_win.show_all()
Gtk.main()
You have the order wrong
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
#some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
win.add(master_win)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
You're trying to add a Gtk.Window() object into Gtk.ScrolledWindow() object and that is not possible. It's inversed.
A Gtk.Window is a toplevel window which can contain other widgets. Windows normally have decorations that are under the control of the windowing system and allow the user to manipulate the window (resize it, move it, close it,…).
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
# some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
# Gtk.ScrolledWindow added to Gtk.Window
win.add(master_win)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Here is my test code (running on Ubuntu 14.04):
try:
from gi.repository import Gtk,Gdk,GObject
except ImportError:
import gtk as Gtk
import gtk.gdk as Gdk
import gobject as GObject
def deiconify( widget ):
print 'deiconifying the window'
widget.deiconify()
return True
win = Gtk.Window()
win.show_all()
#win.iconify()
GObject.timeout_add( 2000, deiconify, win)
Gtk.main()
I just want to deiconify (redisplay) the window after I click the minimize button, but it doesn't work using the codes here. But if I uncomment this line #win.iconify() instead of clicking the minimize button, it will redisplay the window (after that, it still can not deiconify the window if I click the minimize button). Did I miss calling some other functions here? Any help will be appreciated.
I have the same issue with deiconify.
Then I found another function which work as expected.
def deiconify( widget ):
print 'deiconifying the window'
widget.present()
return True
In the following program I have a button that spawns a popup. Simple enough. Now I connect the main window's delete-event to Gtk.main_quit() so that closing the main window closes the program.
Without that it will keep running until I kill the process (As evidenced by the occupied CLI prompt) The question then is: What happens to the popup window when I click it away?
Is the window itself automatically being destroyed at the delete-event or does it just hide itself and linger somewhere in memory until the program ends?
#!/usr/bin/python3
from gi.repository import Gtk
class MainWin(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
button = PopupButton()
self.add(button)
self.show_all();
self.connect("delete-event", Gtk.main_quit)
class PopupButton(Gtk.Button):
def __init__(self):
Gtk.Button.__init__(self, label="Popup")
self.connect("clicked", self.clicked)
def clicked(self, widget):
win = PopupWindow()
win.set_transient_for(self.get_toplevel())
win.show()
class PopupWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.add(Gtk.Label(label="Popups! Popups for everyone!"))
self.show_all()
win = MainWin()
win.show()
Gtk.main()
The default response to the delete-event signal is to destroy the window. So, unless you're handling that signal, the popup window is destroyed.
So, I'm using Python and PyQt. I have a Main Window that contains a QTableWidget, and a dialog that opens modally and has some QLineEdit widgets... All right so far, but I have 2 problems:
When the dialog opens, my Main Window freezes, and I don't really like that...
What I want, when I finish editing a QLineEdit, is that the program will search the QTableWidget, and if the text from the QLineEdit exists in the table, a dialog will come up and informe about that. That's the general idea. But, so far, I seem to only be able to create a new QTableWidget instance, and I can't use the data from the existing...
What can I do about these?
You wrote:
and a dialog that opens modally
and then:
When the dialog opens, my Main Window freezes
The docs say:
int QDialog::exec () [slot]
Shows the dialog as a modal dialog,
blocking until the user closes it. The function returns a DialogCode
result. If the dialog is application modal, users cannot interact with
any other window in the same application until they close the dialog.
If the dialog is window modal, only interaction with the parent window
is blocked while the dialog is open. By default, the dialog is
application modal.
About modeless dialogs:
A modeless dialog is a dialog that operates independently of other
windows in the same application. Find and replace dialogs in
word-processors are often modeless to allow the user to interact with
both the application's main window and with the dialog.
Modeless
dialogs are displayed using show(), which returns control to the
caller immediately.
An example:
import sys
from PyQt4 import QtCore, QtGui
class SearchDialog(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Search')
self.searchEdit = QtGui.QLineEdit()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.searchEdit)
self.setLayout(layout)
class MainWindow(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self, None)
self.resize(QtCore.QSize(320, 240))
self.setWindowTitle('Main window')
self.logText = QtGui.QPlainTextEdit()
searchButton = QtGui.QPushButton('Search')
layout = QtGui.QVBoxLayout()
layout.addWidget(self.logText)
layout.addWidget(searchButton)
self.setLayout(layout)
searchButton.clicked.connect(self.showSearchDialog)
def showSearchDialog(self):
searchDialog = SearchDialog(self)
searchDialog.show()
searchDialog.searchEdit.returnPressed.connect(self.onSearch)
def onSearch(self):
self.logText.appendPlainText(self.sender().text())
def main():
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
if __name__ == "__main__":
main()
Click 'Search' to open a search window (you can open several of them). Enter a text to search and press Enter. The text to search will be added to the log in the main window.