i have a big problem (for me!) with python gtk module.
i can open multi windows; but i can't close singly ( one time , one window ).
if i close a window, all windows close automatically.
i want to close the first window only. after closing firt window, come a new window ( by my choice).
for example :
#!/usr/bin/env python
import pygtk
pygtk.require20()
import gtk
class CLS1(object):
def __init__(self):
self.mywindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.mywindow.connect("delete_event", gtk.main_quit)
self.btn = gtk.Button("Cls1|Btn")
self.mywindow.add(self.btn)
self.mywindow.show_all()
def main(self):
gtk.main()
class CLS2(object):
def __init__(self):
self.mywindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.mywindow.connect("delete_event", gtk.main_quit)
self.btn = gtk.Button("Cls2|Btn")
self.mywindow.add(self.btn)
self.mywindow.show_all()
def main(self):
gtk.main()
class APP(object):
def __init__(self):
self.mywindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.mywindow.connect("delete_event", gtk.main_quit)
self.hori = gtk.HBox()
self.btn1 = gtk.Button("AppBtn1")
self.btn2 = gtk.Button("AppBtn2")
self.btn1.connect("clicked", self.show_me , "AppBtn1")
self.btn2.connect("clicked", self.show_me , "AppBtn2")
self.hori.pack_start(self.btn1)
self.hori.pack_start(self.btn2)
self.mywindow.add(self.hori)
self.mywindow.show_all()
def show_me(self, penar, data):
if data=="AppBtn1" :
CLS1().main()
if data=="AppBtn2":
CLS2().main()
gtk.main_quit()
def main(self):
gtk.main()
APP().main()
i want this :
1- i will run the program
2- the "APP" class will work and will come "APP" window
3- if i click a button (AppBt1 or AppBtn2) ; the "APP" window will close (automatically ; not by me!)
4- if i was clicked "AppBtn1" button on "APP" window (#step 3) ; the "CLS1" class will work and its window will open
,or if i was clicked "AppBtn2" button on "APP" window (#step 3) ; the "CLS2" class will work and its window will open
i wanna only one window on the screen while program running; if i click a button ; its window will close and a new window will open (by my choice, and automatically!)
how can i do this? and can you write its code :)
thanks a lot !
Calling gtk.main_quit will kill the whole program (it basically stop GTK). So what you need, is just to stop GTK when the last window has been closed. What you're currently doing is stopping GTK when any window is closed.
So just use a global variable that you will use as a counter of the windows open. On the delete-event handler, decrement that counter, and if it reached 0 that means you have no more windows open, and you can call gtk.main_quit, otherwise, do nothing and the window will just be normally destroyed.
To kill the parent window, just pass the parent as the last parameter when you connect to the clicked signal. In the associated callback, you'll get that last parameter and call gtk.Widget.destroy on it.
Well a better way might be to modify the window that's already open instead of closing it and opening another.
Related
OS: W10. This may be significant. If you have different results on a different platform, feedback would be helpful.
Here is an MRE. If you run it and go Ctrl+O, the menu labels become greyed. If you select a file in the QFileDialog by clicking the "Open" button or using its mnemonic (Alt+O), the open-file dialog is dismissed and the "Files" and "Help" menus become un-greyed.
However, if you go Ctrl+O again, and this time enter the name of a file in the "File name" box (QLineEdit), and then press Return, the dialog is dismissed (with a successful selection result) but the "Files" and "Help" menus remain greyed-out. It looks like this:
import sys, os
from PyQt5 import QtWidgets, QtCore, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Greying of menus MRE')
self.setGeometry(QtCore.QRect(100, 100, 400, 200))
menubar = QtWidgets.QMenuBar(self)
self.setMenuBar(menubar)
self.files_menu = QtWidgets.QMenu('&Files', self)
menubar.addMenu(self.files_menu)
self.help_menu = QtWidgets.QMenu('&Help', self)
menubar.addMenu(self.help_menu)
self.new_action = QtWidgets.QAction('&New', self)
self.files_menu.addAction(self.new_action)
self.open_action = QtWidgets.QAction('&Open', self)
self.files_menu.addAction(self.open_action)
self.open_action.setShortcut("Ctrl+O")
self.open_action.triggered.connect(self.open_file)
def focusInEvent(self, event ):
print('main_window focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('main_window focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('main_window activateWindow')
super().activateWindow()
def open_file(self):
print('open file')
main_window_self = self
# open_doc_dialog = QtWidgets.QFileDialog(self.get_main_window())
class OpenDocFileDialog(QtWidgets.QFileDialog):
def accepted(self):
print('accepted')
super().accepted()
def accept(self):
print('accept')
super().accept()
def close(self):
print('close')
super().close()
def done(self, r):
print(f'done r {r}')
# neither of these solves the problem:
# main_window_self.activateWindow()
# main_window_self.files_menu.activateWindow()
super().done(r)
def hide(self):
print(f'hide')
super().hide()
def focusInEvent(self, event ):
print('focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('activateWindow')
super().activateWindow()
open_doc_dialog = OpenDocFileDialog(self)
open_doc_dialog.setWindowTitle('Choose file')
open_doc_dialog.setDirectory(os.getcwd())
# we cannot use the native dialog, because we need control over the UI
options = open_doc_dialog.Options(open_doc_dialog.DontUseNativeDialog)
open_doc_dialog.setOptions(options)
open_doc_button = open_doc_dialog.findChild(QtWidgets.QDialogButtonBox).button(QtWidgets.QDialogButtonBox.Open)
lineEdit = open_doc_dialog.findChild(QtWidgets.QLineEdit)
# this does not solve the problem
# lineEdit.returnPressed.disconnect()
# lineEdit.returnPressed.connect(open_doc_button.click)
print(f'open_doc_button {open_doc_button}, lineEdit {lineEdit}')
# show the dialog
dialog_code = open_doc_dialog.exec()
if dialog_code != QtWidgets.QDialog.Accepted: return
sel_files = open_doc_dialog.selectedFiles()
print(f'sel_files: {sel_files}')
app = QtWidgets.QApplication([])
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
This problem can be understood, if not solved, with reference to this answer.
Note that this greying-out is not disablement. As explained in the above link, this has to do with "active/inactive states" of the menus (or their labels). The menus remain enabled throughout, although in this case it's impossible to know that while the open-file dialog is showing because it is modal. Clicking on one menu after the dialog has gone, or just hovering over it, is enough to un-grey them both...
The explanation, as I understand it, is that the "File name" box QLineEdit has a signal, returnPressed, which appears to activate something subtley different to the slot which is invoked when you use the "Choose" button. You can see I have experimented with trying to re-wire that signal, to no avail.
The method done of the QFileDialog appears to be called however the dialog closes (unlike close!), so I tried "activating" the main window... and then the individual QMenus... Doesn't work.
I am not clear how to get a handle on this "active state" business or why the slot connected to returnPressed is (seemingly) unable to give the "active state" back to the menus when the other slot manages to do so.
Edit
Searching on Musicamante's "unpolishing" suggestion led me to this:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
lineEdit.returnPressed.connect(return_pressed)
... unfortunately this doesn't work.
This looks like a possible Windows-related bug, since I can't reproduce it on Linux. As a work-around, you could try forcing a repaint after the dialog closes:
# show the dialog
dialog_code = open_doc_dialog.exec()
self.menubar.repaint()
Finally got it, thanks to Musicamante's suggestion:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
main_window_self.menubar.repaint()
lineEdit.returnPressed.connect(return_pressed)
... I actually tried this several times, just to make sure it was doing what was intended. So in fact, fortunately, no single-shot timer was needed in this case.
when I opened my Top Level window, I always saw an annoying and weird behaviour.. at the end I realized that it was because of my custom icon.
below an exaple code:
from tkinter import *
from tkinter import ttk
class MainWindow:
def __init__(self):
self.parent=Tk()
self.parent.geometry("494x410+370+100")
self.parent.title("My Software - WITH ICON")
self.parent.iconbitmap("icon.ico")
Button = ttk.Button(self.parent, text="open a new widnow", command=self.OpenNewWindow)
Button.place(x=16, y=16)
def OpenNewWindow(self):
self.obj = NewWindow(self)
class NewWindow:
def __init__(self, mw):
self.window, self.mw = Toplevel(mw.parent), mw
self.window.geometry("200x150+360+200")
self.window.title("New Window")
self.window.iconbitmap("icon.ico") # it creates the issue..
self.window.protocol("WM_DELETE_WINDOW", self.on_close)
self.window.focus()
self.mw.parent.attributes('-disabled', 1)
self.window.transient(mw.parent)
self.window.grab_set()
self.mw.parent.wait_window(self.window)
def on_close(self):
self.mw.parent.attributes('-disabled', 0)
self.window.destroy()
def main():
app=MainWindow()
app.parent.mainloop()
if __name__=="__main__":
main()
to make it clear where is the issue, I create a GIF:
here we have two softwares, "without icon.py" and "with icon.py". they are the same, but the first one doesn't use my custom icon for his second window.
as you can see, if I run "with icon.py" the second window will be always affected by something when I will open it, but for "without icon.py" this "something" doesn't exist.
what is the issue? the software opens his second window, focus the root one (it's the issue), and then focus the second window again. you can see it clearly from the GIF.
how can I solve the issue? and why with the default icon this weird behaviour doesn't happen?
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.
I have a main window with a Gtk Button named openDialog. If I click on this button another Window (addName) popups. I would like to write a method (or a function, don't know which is the right name in python) in my main window file, called printHi. I would like to run this printHi method (in my main window file), when addName window is destroyed.
I tried something like this:
def on_addName_destroy():
printHi()
But it doesn't work. Any suggestion?
You can make use of "delete-event" signal of gtk.Widget. It is also possible to make use of "destroy" signal of gtk.Object. Here is a sample which connects to both the signals although in your case connecting to any one of them should suffice.
#!/usr/bin/env python
import gtk
def on_addName_destroy(gtkobject, data=None):
print "This is called later after delete-event callback has been called"
print "Indication that the reference of this object should be destroyed"
print "============================================"
def on_addName_delete(widget, event, data=None):
print "This is called on delete request"
print "Propagation of this event further can be controlled by return value"
print "--------------------------------------------"
return False
def show_popup(widget, data=None):
dialog = gtk.Window(gtk.WINDOW_TOPLEVEL)
dialog.set_size_request(100, 100)
label = gtk.Label("Hello!")
dialog.add(label)
dialog.connect("delete-event", on_addName_delete)
dialog.connect("destroy", on_addName_destroy)
dialog.show_all()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(100, 100)
button = gtk.Button("Popup")
button.connect("clicked", show_popup)
window.add(button)
window.connect("destroy", lambda x: gtk.main_quit())
window.show_all()
gtk.main()
Hope this helps!