Pyqt5 Multiple Windows - How to prevent window switching? - python

I'm making a GUI using pyqt5 and I have two windows. In the first window, there is a button to open the second window. Now, I want to prevent windows switching from the second window to the first one. For example, when we open file dialog, we cannot switch to the main window and the main window is not clickable. (you can check it)
I have tried to set windows flag with :
setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
but it just makes the second window stay on top and I can still switch to the first window. Is there a way to prevent windows switching or make the first window to be not clickable?
Thanks in advance

You can make the window modal by:
setWindowModality(Qt.ApplicationModal)

Related

Python's Tkinter's iconify not working on Fedora

This code is used to hide or dock the current window, and then create a new window using CTkTopLevel, and then to automatically undock or bring back the previous window to the screen. This seems to work on OSX perfectly, but when I try it on my Fedora machine, it fails to undock? Any reason why?
This is the code, called in both machines.
def CreateNewAccount(self):
#hide the previous window // seems to only work on OSX well
self.iconify()
#create new window
window = customtkinter.CTkToplevel(self)
window.title("Project")
window.geometry("1200x800")
window.resizable(False, False)
window.protocol("WM_DELETE_WINDOW", self.deiconify())
It should automatically undock and dock when deleting the window, tried other things like withdraw and it didn't work. Sometimes got a bug where after deleting the second tab, it would stay there as an unreactive tab and would only disappear after I closed the whole program.

Python/Tkinter - How Would I Make a Window More "Important" Than Other Application Windows

I'd like to know how I can have my window as a priority over other application windows.
For instance, even if my window was on top of an existing application window, and I happened to click on the existing application window, I don't want my window to hide behind the existing window, I want it to stay visible (in the foreground) at all times. Is this possible?
(Using overrideredirect, by the way)
For windows you can use topmost
root.wm_attributes("-topmost", 1)

The proper way to display multiple windows in tkinter?

I am currently working on a project using Python and tkinter.
The problem is that I don't know what's the proper way to display multiple windows, or screens, I don't know how to call them. Let me explain better.
When the application starts the login screen appears. After that, if I click register, I want to go to the register screen, but I don't want it to be a separate window (I don't want to have 2 windows displayed at the same time), but rather another window with different content ?!
How should I handle properly this situation? Create a second window using Toplevel and hiding the first (can I do that?) or changing the widgets of the first?
Code I've written so far
You can do that- just call window.withdraw() on the Toplevel you need to hide after creating a new Toplevel. Changing the widgets in the first is also an option- if you like, you could always try a Notebook widget and disable manual flipping or just put each "screen" in a frame and grid_ or pack_forget them to remove them from the window.

How do you switch between two windows in PyQt?

I want to make a new PyQt window show up and hide the current window when I click a button. After playing around for hours, I haven't found a good way to do this.
When I try to show a new QDialog and hide the current one, the program closes. It works if another window is open. I believe this is because it is viewing the last window as closing and terminates, or maybe the new QDialog is falling off the stack.
My current method involves storing the QDialog as a variable in the current dialog, then showing it and hiding current dialog. This seems like a total hack, though.
Example below:
def _createQuestion(self):
# initialization of the UI and everything goes on in the constructor
self.dialog = QuestionBanks()
self.dialog.show()
self.hide()
Is there a better way of switching a single window to a distinct different single window in PyQt4?

How to change the OSX menubar in wxPython without any opened window?

I am writing a wxPython application that remains open after closing all of its windows - so you can still drag & drop new files onto the OSX dock icon (I do this with myApp.SetExitOnFrameDelete(False)).
Unfortunately if I close all the windows, the OSX menubar will only contain a "Help" menu. I would like to add at least a File/Open menu item, or just keep the menubar of the main window. Is this somehow possible in wxPython?
In fact, I would be happy with a non-wxPython hack as well (for example, setting the menu in pyobjc, but running the rest of the GUI in wxPython). wxPython development in OSX is such a hack anyway ;)
UPDATE: I managed to solve this problem using the tip from Lyndsey Ferguson. Here's what I have done:
On startup I create a window which I show and hide immediately. I set its position to (-10000,-10000) so that it does not flicker on the screen (aargh, what a dirty hack!)
I create an empty EVT_CLOSE event handler in that window so that it cannot be closed.
It seems that destroying a window resets the OSX menu, but hiding does not... So when the last window is closed, I need to show and hide this window again (hiding is necessary so that the user cannot switch to this window using the Window menu or Cmd-`)
Yeah, this is really ugly... I will be very grateful if someone comes up with a prettier solution.
UPDATE 2: Actually it can be solved in a much easier way: if we do not close the last window, only hide it. And ensure that it does not respond to menu events anymore.
Nowadays you can use wx.MenuBar.MacSetCommonMenuBar() to set the menu bar (which you have to create) that should be used when no windows are open.
If you just want a default macOS menu bar to be used (with the application and Window menus already there), this appears to be the minimal code:
menubar = wx.MenuBar()
wx.MenuBar.MacSetCommonMenuBar(menubar)
This will let your app respond to Command+Q out-of-the-box, too.
The wx.MenuItem IDs wx.ID_ABOUT and wx.ID_EXIT are special as menu items with those IDs are moved to the macOS Application menu. The docs actually refer to the application menu as the "Apple" menu (e.g. the menu described in the wx.MenuBar.OSXGetAppleMenu() function's docs is the application menu), possibly for historical reasons.
Can you create a hidden window that is offscreen somewhere? It is a hack, but I remember having to do a lot of hacks to make my wxPython-based application work correctly on Mac OS X.
Note:You'll have to disable the close button and set up that hidden window so that it doesn't show up in the Window menu.
Aside:Have you considered factoring out your GUI portion of your Python application and using PyObjC on Mac OS X? You'll get more native behaviours...

Categories