PyQT: how to open new window
Here I'm using the show
method.
Here is a working example (derived from yours):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked
method, like so:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialogs = list()
def on_pushButton_clicked(self):
dialog = Second(self)
self.dialogs.append(dialog)
dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
opening a new window
In the answer of PyQT: how to open new window is using QMainWindow unlike you who is using QWidget.
What is the difference between QMainWindow and QWidget?
QMainWindow
is a custom QWidget
that has some flags activated, including the flag Qt::Window
. As indicated by the docs indicates:
Indicates that the widget is a window, usually with a window system
frame and a title bar, irrespective of whether the widget has a parent
or not. Note that it is not possible to unset this flag if the widget
does not have a parent.
That is, that widget will become a window even if it has a parent widget.
So the solution is to activate this flag in Window2:
import sys
from PyQt5 import QtCore, QtWidgets
class Window2(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window2, self).__init__(parent, QtCore.Qt.Window) # <---
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.init_ui()
def init_ui(self):
self.btn = QtWidgets.QPushButton("Push Me")
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setWindowTitle("PyQt5 double window")
self.btn.clicked.connect(self.btn_clk)
self.newindow = Window2(self)
self.show()
def btn_clk(self):
self.newindow.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())
Other alternative solutions are:
Make
Window2
inherit fromQMainWindow
orQDialog
.Do not pass a parent to Window2:
self.newindow = Window2()
How to open new window with push button
Start using layouts!
A widget without a parent - there is a window.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Window(QWidget): #(QMainWindow):
def __init__(self):
super().__init__()
self.title="Select date from calendar"
self.left, self.top, self.width, self.height = 600, 100, 500, 480
self.iconName = "Ok.png" # <--- home.png
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.calendar = QCalendarWidget()
self.calendar.setGridVisible(True)
self.calendar.selectionChanged.connect(self.onSelectionChanged)
self.label = QLabel()
self.label.setFont(QtGui.QFont("Sanserif", 10))
self.label.setStyleSheet('color: blue;')
self.proceedbutton = QPushButton("Proceed to select time", self)
self.proceedbutton.setToolTip("<h3>Start the Session</h3>")
self.proceedbutton.clicked.connect(self.window2)
self.backbutton = QPushButton("Back", self)
self.backbutton.setToolTip("<h3>Start the Session</h3>")
self.comboBox = None
self.grid = QtWidgets.QGridLayout(self)
self.grid.addWidget(self.calendar, 0, 0, 1, 3)
self.grid.addWidget(self.label, 1, 0, 1, 3)
self.grid.addWidget(self.backbutton, 2, 1, 1, 1)
self.grid.addWidget(self.proceedbutton, 2, 2, 1, 1)
def window2(self):
self.window = QWidget()
self.window.setWindowTitle("Select Time")
self.window.setGeometry(self.left/3, self.top, self.width/3, self.height/3)
self.label = QLabel("Select Time") # --- , self)
self.comboBox = QtWidgets.QComboBox()
self.comboBox.addItems(["choose time", "10", "11", "12"])
self.comboBox.activated[str].connect(self.onComboActivated)
layout = QFormLayout(self.window)
layout.addRow('Choose Time', self.comboBox)
self.window.show()
def onSelectionChanged(self):
ca = self.calendar.selectedDate()
self.label.setText(ca.toString())
def onComboActivated(self, text):
print("choose time: {}".format(text))
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec())
How to open a window with a click of a button from another window using PyQt?
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.
Opening new Window having grid layout in Pyqt5
Try it:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
### global variables
# global memorysize # ---
# global numberofholes # ---
###
class Window(QWidget):
def __init__(self,parent=None):
super(Window,self).__init__(parent)
self.setWindowTitle("Memory 1")
self.setGeometry(50, 50, 500, 300)
self.home()
def home(self):
self.grid = QGridLayout()
self.setLayout(self.grid)
self.memory = QLabel(self)
self.memory.setText("Total Memory size")
self.grid.addWidget(self.memory, 0, 0)
self.memoryinput = QLineEdit(self)
self.grid.addWidget(self.memoryinput, 0, 20)
self.holes = QLabel(self)
self.holes.setText("Number of holes")
self.grid.addWidget(self.holes, 5, 0)
self.inputholes = QLineEdit(self)
self.grid.addWidget(self.inputholes, 5, 20)
self.submit = QPushButton("OK", self)
self.grid.addWidget(self.submit, 10, 0)
# Action on clicking submit
self.submit.clicked.connect(self.getholes)
def getholes(self):
memorysize = float(self.memoryinput.text())
numberofholes = int(self.inputholes.text())
self.hide() # --- close()
self.window2 = Window2(memorysize, numberofholes) # --- self
self.window2.show()
# second window for holes input
class Window2(QWidget): # --- QMainWindow,
def __init__(self, memorysize, numberofholes, parent=None):
super().__init__(parent)
self.memorysize, self.numberofholes = memorysize, numberofholes
print("memorysize=`{}`,\nnumberofholes=`{}`".format(self.memorysize, self.numberofholes))
self.setWindowTitle("Memory 2")
self.setGeometry(50,50,500,300)
self.home()
self.show()
def home(self):
self.grid = QGridLayout()
self.setLayout(self.grid)
print(self.numberofholes)
for n in range (2):
self.start_add = QLabel(self)
self.start_add.setText("Starting Address")
self.inputstart = QLineEdit(self)
self.size = QLabel(self)
self.size.setText("Size")
self.inputsize = QLineEdit(self)
self.grid.addWidget(self.start_add, 2*n+1, 0)
self.grid.addWidget(self.inputstart,2*n+1, 1)
self.grid.addWidget(self.size, 2*n+1, 2)
self.grid.addWidget(self.inputsize, 2*n+1, 3)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
Close and Open new Window PYQT5
There can only be one QApplication
within the PyQt
application, so if you already created it, do not do it again.
Another problem is that the variables exist only within the context, in your case mainWindow, so at the end of the function StartInterface will eliminate this variable and the window, the solution is to make the mainWindow member of the class, so the context will be the class and no longer the function, so it will stay correctly.
def IniciarInterfaz(self):
self.hide()
self.ex = mainWindow()
self.ex.setStyleSheet("background-color: #fff")
self.ex.show()
Related Topics
Rename Specific Column(S) in Pandas
What Version of Visual Studio Is Python on My Computer Compiled With
How to Reverse Lists in Python, Getting "Nonetype" as List
How to Convert a Numpy Array to Pil Image Applying Matplotlib Colormap
Python Method for Reading Keypress
Runtimeerror: Main Thread Is Not in Main Loop
How to Access Pandas Groupby Dataframe by Key
Syntaxerror Inconsistency in Python
When Should I Subclass Enummeta Instead of Enum
Add Column to Dataframe with Constant Value
Pandas: Looking Up the List of Sheets in an Excel File
How to Randomly Choose a Maths Operator and Ask Recurring Maths Questions with It
Forced Naming of Parameters in Python
Python Numpy/Scipy Curve Fitting
Python: Load Variables in a Dict into Namespace
How to Check If One Two-Dimensional Numpy Array Contains a Specific Pattern of Values Inside It