Open a new window when the button is clicked || PyQt5 - python

I know this question have been asked multiple times. But I can't understand with the existing examples.
I have a code which creates a window with a button called 'start'. I want the app to close the current window and open a new window when the 'start' button is clicked. The new window has to be a blank window.
Any help is much appreciated.
The code is as follows:
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton,
QToolTip, QMessageBox, QLabel
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Start", self)
self.pushButton.move(275, 200)
self.pushButton.setToolTip("<h3>Start the Session</h3>")
self.main_window()
def main_window(self):
self.label = QLabel("Manager", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())

Try it:
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QToolTip, QMessageBox, QLabel)
class Window2(QMainWindow): # <===
def __init__(self):
super().__init__()
self.setWindowTitle("Window22222")
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Start", self)
self.pushButton.move(275, 200)
self.pushButton.setToolTip("<h3>Start the Session</h3>")
self.pushButton.clicked.connect(self.window2) # <===
self.main_window()
def main_window(self):
self.label = QLabel("Manager", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
def window2(self): # <===
self.w = Window2()
self.w.show()
self.hide()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())

Related

PyQt5 - Application hangs when launching a separate QMainWindow using pynput

I'm trying to open a separate window using button combinations in pynput. I'm using the following code to open a new window and hide the original QMainWindow object. When the button is pressed, the logic works perfectly. But when I try to bind the same function to a combination of keyboard input, the application hangs. Anyone knows how I should approach this?
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QLabel)
from pynput import keyboard
class Window2(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window22222")
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Start", self)
self.pushButton.move(275, 200)
self.pushButton.setToolTip("<h3>Start the Session</h3>")
self.pushButton.clicked.connect(self.window2)
self.main_window()
def main_window(self):
self.label = QLabel("Manager", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
def hotkeys(self):
def on_activate_h():
self.window2()
def for_canonical(f):
return lambda k: f( l.canonical(k) )
h = keyboard.HotKey( keyboard.HotKey.parse( '<ctrl>+<alt>+h' ), on_activate_h )
l = keyboard.Listener( on_press=for_canonical(h.press), on_release=for_canonical(h.release) )
l.start()
def window2(self):
self.w = Window2()
self.w.show()
self.hide()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.hotkeys()
sys.exit(app.exec())

i can't clicked.connect() QPushButton on QFormLayout [duplicate]

This question already has an answer here:
PyQt TypeError connect()
(1 answer)
Closed 2 years ago.
this is the full code, i don't know why i can't use that clicked.connect on my mind, that's still logic '-' but why!!!???
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
import sys
class Window(QWidget):
def __init__(self, val):
super().__init__()
self.title = "PyQt5 Scroll Bar"
self.top = 200
self.left = 500
self.width = 400
self.height = 300
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
formLayout = QFormLayout()
groupBox = QGroupBox("This Is Group Box")
labelLis = []
comboList = []
for i in range(val):
labelLis.append(QLabel("Label"))
comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
formLayout.addRow(labelLis[i], comboList[i])
groupBox.setLayout(formLayout)
scroll = QScrollArea()
scroll.setWidget(groupBox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout = QVBoxLayout(self)
layout.addWidget(scroll)
self.show()
App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())
and i got error in here, i wanna make every each item can clicked, but i don't know why this happen :"D
for i in range(val):
labelLis.append(QLabel("Label"))
comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
formLayout.addRow(labelLis[i], comboList[i])
groupBox.setLayout(formLayout)
this code not working, comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
I don’t know why comboList.append(QPushButton("Click Me").clicked.connect(self.hello) error occurs
But i will do
qbutton = QPushButton("Click Me")
qbutton.clicked.connect(self.hello)
result
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
import sys
class Window(QWidget):
def __init__(self, val):
super().__init__()
self.title = "PyQt5 Scroll Bar"
self.top = 200
self.left = 500
self.width = 400
self.height = 300
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
formLayout = QFormLayout()
groupBox = QGroupBox("This Is Group Box")
labelLis = []
comboList = []
for i in range(val):
qbutton = QPushButton("Click Me")
qbutton.clicked.connect(self.hello)
labelLis.append(QLabel("Label"))
comboList.append(qbutton)
formLayout.addRow(labelLis[i], comboList[i])
groupBox.setLayout(formLayout)
scroll = QScrollArea()
scroll.setWidget(groupBox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout = QVBoxLayout(self)
layout.addWidget(scroll)
self.show()
def hello(self):
print('hello')
App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())

Pyqt5 Load İmage After Button Clicked

I want to show a image on form after clicking the button.But the code below didn't work. I have defined a function.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
import TurkceOcr as ocr
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Resimden Texte'
self.left = 50
self.top = 50
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create widget
button = QPushButton('Resim Yükle', self)
button.setToolTip('This is load picture button')
button.move(10, 10)
button.clicked.connect(self.on_click)
self.label = QLabel(self)
self.label.move(10,50)
#self.resize(pixmap.width(), pixmap.height())
self.show()
#pyqtSlot()
def on_click(self):
print('PyQt5 button click')
image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg)")
imagePath = image[0]
pixmap = QPixmap(imagePath)
self.label.setPixmap(pixmap)
#print(ocr.resimden_yaziya(imagePath))
print(imagePath)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
QWidget::adjustSize()
Adjusts the size of the widget to fit its contents.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
#import TurkceOcr as ocr
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Resimden Texte'
self.left = 50
self.top = 50
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create widget
button = QPushButton('Resim Yükle', self)
button.setToolTip('This is load picture button')
button.move(10, 10)
button.clicked.connect(self.on_click)
self.label = QLabel(self)
self.label.move(10,50)
#self.resize(pixmap.width(), pixmap.height())
self.show()
#pyqtSlot()
def on_click(self):
print('PyQt5 button click')
image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg)")
imagePath = image[0]
pixmap = QPixmap(imagePath)
self.label.setPixmap(pixmap)
self.label.adjustSize() # <---
#print(ocr.resimden_yaziya(imagePath))
print(imagePath)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

How to change the size of PyQt5 directory view in the main window?

I am working on a PyQt5 project, which needs a folder viewer by PyQt5 QTreeView. In order to put more stuff, I try to change the size of the tree view but in vain. Here is the code from Pythonspot:
import sys
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
from PyQt5.QtGui import QIcon
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 file system view - pythonspot.com'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.model = QFileSystemModel()
self.model.setRootPath('')
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.setSortingEnabled(True)
self.tree.setWindowTitle("Dir View")
self.tree.resize(640, 200)
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.tree)
self.setLayout(windowLayout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
I change the tree view by
self.tree.resize(640, 200)
Why it does not function?
A layout is used to establish the position and size of the widget you are using, so in your case even if you use resize the size will not be changed, instead you should set a fixed size so the layout can not change the size of the QTreeView.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 file system view - pythonspot.com'
self.left, self.top, self.width, self.height = 10, 10, 640, 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath('')
self.tree = QtWidgets.QTreeView()
self.tree.setModel(self.model)
self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.setSortingEnabled(True)
self.tree.setWindowTitle("Dir View")
self.tree.setFixedSize(640, 200)
windowLayout = QtWidgets.QVBoxLayout(self)
windowLayout.addWidget(self.tree, alignment=QtCore.Qt.AlignTop)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

PyQT5: Grid layout inside horizontal layout

Can someone help me figure out how to combine layouts?
Taking offset from the guides from: [https://pythonspot.com/en/pyqt5/]
I would rather not use Designer as the layout is going to be a part of several tabs that is determined based on the amount of tests and data sets from a specified data folder.
For example, I would like to switch out the Blue button in:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QDialog):
def __init__(self):
super().__init__()
self.title = 'PyQt5 layout - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 100
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createHorizontalLayout()
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)
self.show()
def createHorizontalLayout(self):
self.horizontalGroupBox = QGroupBox("What is your favorite color?")
layout = QHBoxLayout()
buttonBlue = QPushButton('Blue', self)
buttonBlue.clicked.connect(self.on_click)
layout.addWidget(buttonBlue)
buttonRed = QPushButton('Red', self)
buttonRed.clicked.connect(self.on_click)
layout.addWidget(buttonRed)
buttonGreen = QPushButton('Green', self)
buttonGreen.clicked.connect(self.on_click)
layout.addWidget(buttonGreen)
self.horizontalGroupBox.setLayout(layout)
#pyqtSlot()
def on_click(self):
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
with the grid layout from:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QDialog):
def __init__(self):
super().__init__()
self.title = 'PyQt5 layout - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 100
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createGridLayout()
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)
self.show()
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox("Grid")
layout = QGridLayout()
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)
layout.addWidget(QPushButton('1'),0,0)
layout.addWidget(QPushButton('2'),0,1)
layout.addWidget(QPushButton('3'),0,2)
layout.addWidget(QPushButton('4'),1,0)
layout.addWidget(QPushButton('5'),1,1)
layout.addWidget(QPushButton('6'),1,2)
layout.addWidget(QPushButton('7'),2,0)
layout.addWidget(QPushButton('8'),2,1)
layout.addWidget(QPushButton('9'),2,2)
self.horizontalGroupBox.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
You can add a layout as an element of another layout in a similar fashion to adding a widget by using addLayout()
layout = QtWidgets.QHBoxLayout()
sublayout = QtWidgets.QGridLayout()
layout.addLayout(sublayout)

Categories

Resources