This question already has answers here:
QMdiArea not adding subwindow
(2 answers)
Closed 1 year ago.
I have a problem when using QMdiArea in PyQt5, the following is what I have done:
I create a demo UI with Qt designer, looks like:
When I preview it in Qt designer, it looks OK:
However, when I use pyqt5_uic to convert the .ui file to .py file, and run the code, it looks like this:
I found that the corresponding .py code generate with uic tools fails to set parent for subwindows in mdiarea:
self.mdiArea = QtWidgets.QMdiArea(IOWidgets)
self.mdiArea.setGeometry(QtCore.QRect(555, 120, 421, 181))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.lineEdit_1_IO = QtWidgets.QLineEdit(self.subwindow)
self.lineEdit_1_IO.setGeometry(QtCore.QRect(20, 20, 61, 21))
self.lineEdit_1_IO.setObjectName("lineEdit_1_IO")
self.spinBox_1_IO = QtWidgets.QSpinBox(self.subwindow)
self.spinBox_1_IO.setGeometry(QtCore.QRect(30, 60, 42, 22))
self.spinBox_1_IO.setObjectName("spinBox_1_IO")
if I change the critical line code to:
self.subwindow = QtWidgets.QWidget(self.mdiArea)
then I can see the subwindow in mdiarea, but it looks strange:
here is simple demo:
the .py code generate with uic tools is Ui_Mdi_simple.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MDI_test(object):
def setupUi(self, MDI_test):
MDI_test.setObjectName("MDI_test")
MDI_test.resize(545, 336)
self.mdiArea = QtWidgets.QMdiArea(MDI_test)
self.mdiArea.setGeometry(QtCore.QRect(70, 50, 331, 221))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.comboBox = QtWidgets.QComboBox(self.subwindow)
self.comboBox.setGeometry(QtCore.QRect(50, 30, 68, 22))
self.comboBox.setObjectName("comboBox")
self.lineEdit = QtWidgets.QLineEdit(self.subwindow)
self.lineEdit.setGeometry(QtCore.QRect(50, 70, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.radioButton = QtWidgets.QRadioButton(self.subwindow)
self.radioButton.setGeometry(QtCore.QRect(50, 110, 95, 20))
self.radioButton.setObjectName("radioButton")
self.subwindow_2 = QtWidgets.QWidget()
self.subwindow_2.setObjectName("subwindow_2")
self.retranslateUi(MDI_test)
QtCore.QMetaObject.connectSlotsByName(MDI_test)
def retranslateUi(self, MDI_test):
_translate = QtCore.QCoreApplication.translate
MDI_test.setWindowTitle(_translate("MDI_test", "Form"))
self.subwindow.setWindowTitle(_translate("MDI_test", "subwindow_1"))
self.radioButton.setText(_translate("MDI_test", "RadioButton"))
self.subwindow_2.setWindowTitle(_translate("MDI_test", "subwindow_2"))
the main code is:
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from Ui_Mdi_simple import Ui_MDI_test
class MDI_demo(QWidget, Ui_MDI_test):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MDI_demo()
win.show()
sys.exit(app.exec_())
the results turns out to be:
I found adding the following two lines of codes in Ui_MDI_test solves the problem:
self.mdiArea.addSubWindow(self.subwindow)
self.mdiArea.addSubWindow(self.subwindow_2)
# eyllanesc, thanks.
Related
I'm trying to open QFileDialog widget when I click the push button on the main window.
but after I run the code QfileDialog widget is opened without main window widget.
Below is my code :
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtWidgets
import sys
class Python_Install(QtWidgets.QWidget) :
def __init__(self):
super().__init__()
self.setWindowTitle('main_window')
self.resize(800, 600)
self.initUI()
def initUI(self):
# add GroupBox
self.gb = QtWidgets.QGroupBox(self)
self.gb.setGeometry(QtCore.QRect(40, 120, 731, 331))
self.gb.setTitle('Select File')
# LB_host + LE1(LineEdit) + FC1(File Choose)
self.LB_host = QtWidgets.QLabel(self.gb)
self.LB_host.setGeometry(QtCore.QRect(50, 70, 121, 18))
self.LB_host.setText('host file :')
self.LE1 = QtWidgets.QLineEdit(self.gb)
self.LE1.setGeometry(QtCore.QRect(200, 60, 301, 25))
self.LE1.setReadOnly(True)
self.FC1 = QtWidgets.QPushButton(self.gb)
self.FC1.setGeometry(QtCore.QRect(510, 60, 112, 34))
self.FC1.clicked.connect(self.File_Choose(self.LE1))
def File_Choose(self, X):
file, check = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File', "", "All Files (*);;Text Files (*.txt)")
if check:
X.setText(file)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mw = Python_Install()
mw.show()
sys.exit(app.exec_())
I think this part of the code is wrong...
self.FC1.clicked.connect(self.File_Choose(self.LE1))
def File_Choose(self, X):
file, check = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File', "", "All Files (*);;Text Files (*.txt)")
if check:
X.setText(file)
Because If I change that part as below, It works as I indented :
self.FC1.clicked.connect(self.File_Choose)
def File_Choose(self):
file, check = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File', "", "All Files (*);;Text Files (*.txt)")
if check:
self.LE1.setText(file)
But... what I want is passing the QLineEdit object to 'File_Choose' slot when 'push button' clicked event occurs and writing the chosen file name to the QLineEdit object.
how could I fix it...?
I'm trying to open second window on main window.
I made 2 classes for each windows, and run the code. when I click the button on main window, the main window is closed and second windows show. but after 1 seconds the second window is closed and the process ends.
Really I don't know why it does...
from PyQt5 import QtWidgets
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(400, 300)
# Button
self.button = QtWidgets.QPushButton(self)
self.button.setGeometry(0, 0, 400, 300)
self.button.setText('Open Sub Window')
self.button.setStyleSheet('font-size:40px')
self.button.clicked.connect(self.sub_show)
def sub_show(self):
self.hide()
self.sub_window = SubWindow()
self.sub_window.exec()
self.show()
class SubWindow(QtWidgets.QWidget):
def __init__(self):
super(SubWindow, self).__init__()
self.resize(400, 300)
self.button2 = QtWidgets.QPushButton(self)
self.button2.setGeometry(0, 0, 400, 300)
self.button2.setText('Back to Main window')
self.button2.setStyleSheet('font-size:40px')
self.button2.clicked.connect(self.home)
self.show()
def home(self):
self.close()
if __name__ == "__main__" :
app = QtWidgets.QApplication([])
mw = MainWindow()
mw.show()
sys.exit(app.exec())
You are calling show a few to many times. Also QWidget doesn't have an exec method. You will also want the first window to reappear when you close the second I assume.
I made some changes in the example below. Give it a try:
from PyQt5 import QtWidgets
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(400, 300)
self.button = QtWidgets.QPushButton(self)
self.button.setGeometry(0, 0, 400, 300)
self.button.setText('Open Sub Window')
self.button.setStyleSheet('font-size:40px')
self.button.clicked.connect(self.sub_show)
def sub_show(self):
self.hide()
self.sub_window = SubWindow()
# connect signal to show again when button2 is pressed
self.sub_window.button2.clicked.connect(self.show)
self.sub_window.show()
class SubWindow(QtWidgets.QWidget):
def __init__(self):
super(SubWindow, self).__init__()
self.resize(400, 300)
self.button2 = QtWidgets.QPushButton(self)
self.button2.setGeometry(0, 0, 400, 300)
self.button2.setText('Back to Main window')
self.button2.setStyleSheet('font-size:40px')
self.button2.clicked.connect(self.close)
if __name__ == "__main__" :
app = QtWidgets.QApplication([])
mw = MainWindow()
mw.show()
sys.exit(app.exec())
This question already has answers here:
QtDesigner changes will be lost after redesign User Interface
(2 answers)
Closed 2 years ago.
I have been trying to figure this out for the last two days. I want to make my window frameless, I saw in a couple of resources that I should be using Qt.FramelessWindowHint . However, it doesn't want to work and I can't figure out why.
Can someone please point out what's wrong with my code, as I am very new to PyQt5?
(I have commented out sections that are related to some other files)
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QTimer, Qt
#from HomeWindow import Ui_Form
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
#def secondscreen(self):
#self.Form = QtWidgets.QWidget()
#self.ui = Ui_Form()
#self.ui.setupUi(self.Form)
#self.Form.showMaximized()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(560, 350)
MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
flags = Qt.WindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setWindowFlags(flags)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(80, 280, 470, 60))
self.textBrowser.setFrameShape(QtWidgets.QFrame.NoFrame)
self.textBrowser.setFrameShadow(QtWidgets.QFrame.Plain)
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 270, 61, 71))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../Media/College Logo.png"))
self.label.setScaledContents(True)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 60, 220, 80))
font = QtGui.QFont()
font.setPointSize(28)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(290, 140, 81, 16))
font = QtGui.QFont()
font.setPointSize(9)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(0, 20, 231, 211))
self.label_4.setText("")
self.label_4.setPixmap(QtGui.QPixmap("../Media/Smample Logo.png"))
self.label_4.setScaledContents(True)
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 190, 93, 28))
self.pushButton.setObjectName("pushButton")
#self.pushButton.clicked.connect(self.secondscreen)
#self.pushButton.clicked.connect(MainWindow.close)
#QTimer.singleShot (5000, self.secondscreen)
#QTimer.singleShot (5001, MainWindow.close)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'MS Shell Dlg 2\';\">(c) 2020 SQU, Inc. Protected by international patents. See Squ.om/patents. App name is a registered trademark of SQU, Inc. Other product or brand names may be trademarks or registred trademarks of their respective holder.</span></p></body></html>"))
self.label_2.setText(_translate("MainWindow", "Mechanica"))
self.label_3.setText(_translate("MainWindow", "Version 1.0"))
self.pushButton.setText(_translate("MainWindow", "Start"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You shouldn't try to edit or mimic the behavior of files generated by pyuic, as doing it leads to confusion about the object structure, and that's exactly your case.
Your Ui_MainWindow already inherits from QMainWindow, there's no use to create an instance of QMainWindow. In fact, what you're seeing is that QMainWindow, while you're setting the flag on the Ui_MainWindow instance, which you're never actually showing.
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# ...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = Ui_MainWindow()
mainWindow.show()
sys.exit(app.exec_())
How can I compute the net price (Price * (1 - discount)) automatically whenever I enter data? I can make it work on a button press but I'd like to do it whenever data is entered.
See image below.
Thank you in advance!
At first it seems like you are using QLineEdit for the input. Because you want to input integers, I would recommend using the QSpinBox or QDoubleSpinBox, which are made for the input of integers.
I built up such a GUI like you have with QtCreator and converted it with pyuic5 -x gui.ui -o gui.py on my Raspberry Pi to Python.
You simply need to connect the value change of the SpinBox with a function that gives out the net price. For doing this, see the code below:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 415)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 40, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 140, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 260, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_netprice = QtWidgets.QLabel(self.centralwidget)
self.label_netprice.setGeometry(QtCore.QRect(260, 282, 421, 41))
self.label_netprice.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label_netprice.setText("")
self.label_netprice.setObjectName("label_netprice")
self.doubleSpinBox_price = QtWidgets.QDoubleSpinBox(self.centralwidget)
self.doubleSpinBox_price.setGeometry(QtCore.QRect(260, 60, 421, 41))
self.doubleSpinBox_price.setObjectName("doubleSpinBox_price")
####################################
# Link to function when value changes
self.doubleSpinBox_price.valueChanged.connect(self.Refresh)
####################################
self.doubleSpinBox_discount = QtWidgets.QDoubleSpinBox(self.centralwidget)
self.doubleSpinBox_discount.setGeometry(QtCore.QRect(260, 160, 421, 41))
self.doubleSpinBox_discount.setObjectName("doubleSpinBox_discount")
####################################
# Link to function when value changes
self.doubleSpinBox_discount.valueChanged.connect(self.Refresh)
####################################
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Price:"))
self.label_2.setText(_translate("MainWindow", "Discount:"))
self.label_3.setText(_translate("MainWindow", "Net Price:"))
##########################################
# Function for refreshing the output label
def Refresh(self):
price = self.doubleSpinBox_price.value()
discount = self.doubleSpinBox_discount.value()
netprice = price * (1 - discount)
self.label_netprice.setText(str(netprice))
##########################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
In my code the SpinBoxes are limited at 99 but you can set this to a higher value.
Just a hint: People are more likely to help you, if you give them a short but relatable code example to work with and they don't have to build their own GUI.
I hope this helps. If you find it useful, please mark it as correct.
Best wishes,
RaspiManu
I am trying to integrate simplecv to pyqt4 with some mixed success. I was able to see a webcam capture in pyqt4 through simplecv, I can modify the image with simplecv and it shows ok in pyqt4 but when I try to add a geometry or text to the image it is not showing in pyqt4. If I run the simpleCV code on their own it works OK. Can someone help me understand why it is not working? By the way, as you can see I am new to pyqt4 and simpleCV. See the code that I currently have.
#!/usr/bin/env python
import os
import sys
import signal
from PyQt4 import uic, QtGui, QtCore
from webcamGUI3 import *
from SimpleCV import *
class Webcam(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self,parent)
self.MainWindow = Ui_MainWindow()
self.MainWindow.setupUi(self)
self.webcam = Camera(0,{ "width": 640, "height": 480 })
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.show_frame)
self.timer.start(1);
def show_frame(self):
ipl_image = self.webcam.getImage()
ipl_image.dl().circle((150, 75), 50, Color.RED, filled = True)
data = ipl_image.getBitmap().tostring()
image = QtGui.QImage(data, ipl_image.width, ipl_image.height, 3 * ipl_image.width, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap()
pixmap.convertFromImage(image.rgbSwapped())
self.MainWindow.lblWebcam.setPixmap(pixmap)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
webcam = Webcam()
webcam.show()
app.exec_()
Any ideas?
I got my friend, is very simple you just add
ipl_image = ipl_image.applyLayers()
see:
ipl_image = ipl_image = self.webcam.getImage().binarize().invert()
ipl_image.drawRectangle(30,50,100,100,color=Color.RED,width=3)
ipl_image.drawText('ola galera',80,190,fontsize=50)
ipl_image = ipl_image.applyLayers()
data = ipl_image.getBitmap().tostring()
image = QtGui.QImage(data, ipl_image.width, ipl_image.height, 3 * ipl_image.width, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap()
pixmap.convertFromImage(image.rgbSwapped())
self.MainWindow.label.setPixmap(pixmap)