Open window application in current window - python

I want to open a game application replacing the menu window.
So I have a game application which I can start with:
subprocess.call(["mygameprogram", "argument one"])
This opens up another window, but I want to replace the qt window with the game window without closing the menu window.
This is my MainWindow class:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initMenu()
self.show()
def initMenu(self):
# load ui file
uic.loadUi("somefile.ui", self)
# add pushbutton
btn = QPushButton()
# add click listener
btn.clicked.connect(self.startLevel)
# add button to layout
self.layout_buttons.addWidget(btn)
def startLevel(self):
# open game, put the right code in here :D
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
Is there any solution for that? And if not, what should I do instead?
Thanks in advance

Related

How to quit Qt application before main window is displayed

On launch, my Qt application displays a dialog box with some options, before displaying the main window. The dialog box has a 'Start' and 'Cancel' button. The same dialog box is used at a later time, after the main window is displayed, when the user clicks on a 'New Game' button.
I'm trying to have the 'Cancel' button quit the application if it's the only interface element being displayed (i.e. on application launch). In my current code, however, the main window is still displayed.
If I replace self.view.destroy() with self.view.deleteLater() the main window briefly flashes into existence before disappearing (and quitting properly), but this can't be the solution.
If I move the view.show() call inside the dialog.exec_() block, it doesn't work either. Mainly because I would be calling view.show() every time the dialog is displayed again from within the main window, but also because even in this case the app doesn't really quit. The main window, in this case, is not displayed but the process is keeps running (Python application icon still visible in the Dock).
What am I doing wrong here? I've read other similar questions but I don't understand how to apply these solutions in my case.
(PySide2 5.15.1 on macOS 10.15.6)
class App:
def __init__(self, app, game, view):
self.app = app
self.game = game
self.view = view
# Display the dialog
# Same callback is bound to a QPushButton in MainWindow
self.cb_start_dialog()
def cb_start_dialog(self):
# Do some initialisation
dialog = DialogNewGame() # A subclass of QDialog
if dialog.exec_():
# Setup the interface
else:
if not self.view.isVisible():
# Condition evaluates correctly
# (False on app launch,
# True if main window is displayed)
self.view.destroy()
self.app.quit() # Doesn't work, main window still displayed
def main():
application = QApplication()
view = MainWindow() # A QWidget with the main window
model = Game() # Application logic
App(application, model, view)
view.show()
application.exec_()
If the code is analyzed well, it is observed that "quit" is invoked before the eventloop starts, so it makes no sense to terminate an eventloop that never started. The solution is to invoke X an instant after the eventloop starts. On the other hand, the quit method is static so it is not necessary to access "self.app"
from PySide2.QtCore import QTimer
from PySide2.QtWidgets import QApplication, QDialog, QMainWindow
class MainWindow(QMainWindow):
pass
class DialogNewGame(QDialog):
pass
class Game:
pass
class App:
def __init__(self, game, view):
self.game = game
self.view = view
QTimer.singleShot(0, self.cb_start_dialog)
def cb_start_dialog(self):
dialog = DialogNewGame()
if dialog.exec_():
pass
else:
QApplication.quit()
def main():
application = QApplication()
view = MainWindow()
model = Game()
app = App(model, view)
view.show()
application.exec_()
if __name__ == "__main__":
main()

In python, how do I open a new window by clicking a button in a main window using .py files created from .ui files made in QtDesigner?

I have two .py files created from .ui files made in QtDesigner. One is a main window for the program, and the other is a widget that allows users to input information and I'm calling it the "evaluation" widget.
I have a button in the main window called "btn_StartNew", which is meant to open the widget window to allow the user to start a new evaluation. I'm having troubles with opening the main window, and adding the function to the "btn_StartNew" button that opens the "evaluation" widget.
The code I have right now opens both the main window and the evaluation widget in the same window when I run the code:
import sys
from PyQt4 import QtCore, QtGui
from wid_Main import Ui_wid_Main
from wid_Eval_v2 import Ui_wid_Eval
class Win_Eval(QtGui.QWidget):
def __init__(self, parent=None):
super(Win_Eval, self).__init__(parent)
self.ui=Ui_wid_Eval()
self.ui.setupUi(self)
class Win_Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Win_Main, self).__init__(parent)
self.ui=Ui_wid_Main()
self.ui.setupUi(self)
#RUN "start new eval" button
self.ui.btn_StartNew.clicked.connect(self.btn_StartNew_click)
self.dialog = Win_Eval(self)
def btn_StartNew_click(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = Win_Main()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Where wid_Main and Ui_wid_Main are the main window file and the main window class, respectively. And wid_Eval_v2 and Ui_wid_Eval are the evaluation widget file and class, respectively.
Any help would be much appreciated!
You must change:
self.dialog = Win_Eval(self)
to
self.dialog = Win_Eval()

How to open a window with a click of a button from another window using PyQt?

I'm trying to make an application but I keep getting punched by the "simple" things like this one, how do I open a new window though a button click? I tried using new_lib_btn.clicked.connect(newlib), newlib is the file that contains my second window and new_lib_btn is the button that should open the window, it's in my main window as you can see down here:
mainwindow.py
from PyQt4 import QtCore, QtGui
import newlib
import sys
# Main Window
class Window (QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
centralwidget = QtGui.QWidget(self)
self.mainLayout = QtGui.QVBoxLayout(centralwidget)
self.mainLayout.setAlignment(QtCore.Qt.AlignCenter)
self.setCentralWidget(centralwidget)
self.resize(800, 600)
self.setWindowTitle("Virtual Library")
self.setStyleSheet("Window {border-image: url(lib.jpg);}")
# ExitOption
menu_action1 = QtGui.QAction("Exit", self)
menu_action1.setShortcut("Ctrl+Q")
menu_action1.setStatusTip('Exit The App')
menu_action1.triggered.connect(self.close_application)
self.statusBar()
# MenuBar
main_menu = self.menuBar()
file_menu = main_menu.addMenu('Options')
file_menu.addAction(menu_action1)
self.home()
def home(self):
# NewLibrary btn
new_lib_btn = QtGui.QPushButton("New Library", self)
new_lib_btn.setGeometry(QtCore.QRect(310, 180, 141, 41))
new_lib_btn.setStyleSheet("color: black;")
# AccessLibrary btn
access_lib_btn = QtGui.QPushButton("Access Library", self)
access_lib_btn.setGeometry(QtCore.QRect(310, 250, 141, 41))
access_lib_btn.setStyleSheet("color: black;")
# FindNewBooks btn
find_nbooks = QtGui.QPushButton("Find New Books*", self)
find_nbooks.setGeometry(QtCore.QRect(310, 320, 141, 41))
find_nbooks.setStyleSheet("color: black;")
self.mainLayout.addWidget(new_lib_btn)
self.mainLayout.addWidget(access_lib_btn)
self.mainLayout.addWidget(find_nbooks_btn)
self.show()
def close_application(self):
choice = QtGui.QMessageBox.question(self, 'Exit',
"Close the application?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
And here's my second window, the one I want to open with the new_lib_btn
newlib.py
class NewLibrary (QtGui.QMainWindow):
def __init__(self):
super(NewLibrary, self).__init__()
self.resize(800,600)
self.setWindowTitle("New Library")
self.setStyleSheet("NewLibrary {border-image: url(wood.jpg);}")
# File Options
file_action1 = QtGui.QAction("New Library", self)
file_action1.setShortcut("Ctrl+N")
file_action1.setStatusTip("Creates a new library")
file_action2 = QtGui.QAction("Exit this!", self)
file_action2.setShortcut("Ctrl+Q")
file_action2.setStatusTip("Closes The App")
file_action2.triggered.connect(self.close_application)
#File Menu
main_menu = self.menuBar()
file_menu = main_menu.addMenu("File")
file_menu.addAction(file_action1)
file_menu.addAction(file_action2)
self.newLib()
self.newLib()
def newLib(self):
centralwidget = QtGui.QWidget(self)
self.mainLayout = QtGui.QVBoxLayout(centralwidget)
self.mainLayout.setAlignment(QtCore.Qt.AlignCenter)
#some useful buttons in the future
self.setCentralWidget(centralwidget)
self.show()
def close_application(self):
choice = QtGui.QMessageBox.question(self, 'Exit',
"Close the application?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
def runNewLib():
app = QtGui.QApplication(sys.argv)
gui = NewLibrary()
sys.exit(app.exec_())
runNewLib()
I searched a lot about this but I couldn't understand the few ones that were somewhat close to my situation, so I'm asking for help, It seems so simple but I'm not getting it :/, what should I do to open the second window by clicking new_lib_btn? pls help.
I think there are several issues with the code that you've posted. First, there are two calls to self.newLib() in the NewLibrary constructor. Second, you probably want to put that call to runNewLib() at the bottom of newlib.py behind an if __name__... block, like so:
if __name__ == '__main__':
runNewLib()
Otherwise, every time you try to import newlib.py, it will attempt to run NewLibrary as a separate application.
Getting to the question you asked, I don't think you actually want to call self.show() in either Window.home() or NewLibrary.newLib(). A more typical pattern would be to create an instance of either Window or NewLibrary and then call show() on that instance. So, in your Window class, you'd add a function to create an instance of NewLibrary and then call show on it, like this
def create_new_library_window(self):
self.new_lib = newlib.NewLibrary()
self.new_lib.show()
Note that, as ekhumoro points out, you have to keep a reference to new_lib around, otherwise it will get garbage collected when the function exits. Then in NewLibrary.home() after you've created the new_lib_btn connect it to this new function:
new_lib_btn.clicked.connect(self.create_new_library_window)
Working example
This example creates a main window with one big button that, when clicked will open a second window. It uses two classes that inherit from QMainWindow, as in your question. First, in main.py:
from PyQt4 import QtGui
from new_window import NewWindow
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self._new_window = None
self._button = QtGui.QPushButton('New Window', self)
self._button.clicked.connect(self.create_new_window)
self.setCentralWidget(self._button)
def create_new_window(self):
self._new_window = NewWindow()
self._new_window.show()
if __name__ == '__main__':
app = QtGui.QApplication([])
gui = Window()
gui.show()
app.exec_()
The __init__ function creates a button and connects it to the create_new_window function. When the button is clicked, create_new_window will be called. Inside of create_new_window, we create an instance of NewWindow and assign it to a class member to maintain a reference to the window and prevent it from being garbage collected. We then call show on this new window to display it.
At the bottom, we use the usual if __name__ == '__main__': pattern to control whether this file runs the application or not. If this file is executed from the command line (like python main.py) __name__ == '__main__' evaluates to true and the GUI application will be started. This has the advantage of allowing the file to serve a dual purpose: it can be imported as a standard python package, or executed as an application, all using the same file.
Then in new_window.py:
from PyQt4 import QtGui
class NewWindow(QtGui.QMainWindow):
def __init__(self):
super(NewWindow, self).__init__()
self._new_window = None
self._label = QtGui.QLabel('Hello, is it me you\'re looking for?')
self.setCentralWidget(self._label)
if __name__ == '__main__':
app = QtGui.QApplication([])
gui = NewWindow()
gui.show()
app.exec_()
This file defines a second QMainWindow that uses a label as its central widget. It also uses the __name__ == '__main__' pattern; this file can also be executed as a standalone application or imported as in main.py above.

Communication between mianwindow and dialog in python

Hello I am working on data archive program with python 2.7. I have one mainWindow and there is some elements (Buttons, text lines etc.) Clicking a button open a dialog form page. User select their answer on that dialog page. Dialog page has a button named 'save'. When clicking save button dialog class saving user's selections to database. I want to do, when user clicked to save button on dialog, It will enable some elements on the mainWindow which is not enabled. I am doing this with these codes entering to dialog class and save button function:
self.ui.onceBut.setEnabled(True) etc.
But I am taking an error:
AttributeError: 'onceDlg' object has no attribute 'onceBut'
onceDlg is dialog pages class name.
How can I solve this and I can do what I want? Thanks in advance.
self in self.ui.onceBut.setEnabled(True) refers to dialog, so you get error because your onceBut is in your mainWindow not in onceDlg dialog.
Solution: as #Radio say - Communication between components in Qt are often done using signals and slots, but it's not the only way.
Easier way is to simply pass main window to dialog window, so you can manipulate with it's buttons, or whatever you want, inside dialog window. In next example I've done that in line dialog = Dialog(self), where self refers to MainWindow and it is used as mainWin inside Dialog. Run it, click on first button, dialog will show, click save button in dialog and second button in main will be changed and disabled.
I hope I've helped you.
import sys
from PyQt4 import QtCore, QtGui
class Dialog(QtGui.QDialog):
def __init__(self, mainWin):
QtGui.QDialog.__init__(self,mainWin)
self.setWindowTitle(self.tr("Dialog window"))
self.main = mainWin
button = QtGui.QPushButton()
button.setText( "Save (disable dummy button)" )
layout = QtGui.QVBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
self.connect(button, QtCore.SIGNAL("clicked()"), self.save)
self.resize(200, 100)
def save(self):
self.main.button2.setEnabled(False)
self.main.button2.setText( "changed from dialog" )
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle(self.tr("Main window"))
self.button1 = QtGui.QPushButton()
self.button2 = QtGui.QPushButton()
self.button1.setText( "Open dialog" )
self.button2.setText( "Dummy" )
layout = QtGui.QVBoxLayout()
layout.addWidget(self.button1)
layout.addWidget(self.button2)
self.window = QtGui.QWidget()
self.window.setLayout(layout)
self.setCentralWidget(self.window);
self.connect(self.button1, QtCore.SIGNAL("clicked()"), self.showDialog)
self.resize(360, 145)
def showDialog(self):
dialog = Dialog(self)
dialog.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show();
sys.exit(app.exec_())

PyQt: Accesing Main Window's Data from a dialog?

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.

Categories