I'm trying to get a widget created in Qt designer to be called from a menu option from my QMainWindow class in a different script. So far it just exits instantly. I was wondering if anybody could tell me what I'm doing wrong? Do I need to call a subprocess?
Here is the relevant parts of the scripts:
Main Script:
import sys
from PyQt4 import QtGui
from lib import Step1_Import_data
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
openEditor = QtGui.QAction("&Editor",self)
openEditor.setShortcut("Ctrl+E")
openEditor.triggered.connect(self.editor)
self.statusBar()
mainMenu = self.menuBar()
editorMenu = mainMenu.addMenu('&Editor')
editorMenu.addAction(openEditor)
self.showMaximized()
def editor(self):
Form = QtGui.QWidget()
ui = Step1_Import_Data.Ui_Form()
widget = ui.setupUi(Form)
Form.show()
self.setCentralWidget(widget)
def main():
app = QtGui.QApplication(sys.argv)
GUI = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
And then this is my script Step1_Import_Data.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(826, 536)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(680, 10, 131, 41))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
"""
Loads more stuff...
"""
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Step 2: Data Checking", None))
"""
More stuff redacted to be concise
"""
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
You need to keep a reference to the child window, otherwise it will be immediately garbage-collected when the function returns:
class MainWindow(QtGui.QMainWindow):
def __init__(self):
...
self.editorWindow = None
def editor(self):
if self.editorWindow is None:
self.editorWindow = EditorWindow()
self.editorWindow.show()
class EditorWindow(QtGui.QWidget):
def __init__(self):
super(EditorWindow, self).__init__()
self.ui = Step1_Import_data.Ui_Form()
self.ui.setupUi(self)
Related
I do not get why the text of my label is not updating after clicking a button. If I want to show the output on the console, there is no problem.
import sys
from qtpy import QtWidgets
from UI.mainwindow import Ui_MainWindow
app = QtWidgets.QApplication(sys.argv)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.calc_btn.clicked.connect(self.on_calc_btn_click)
def on_calc_btn_click(self):
weight = int(self.ui.weight_textbox.text())
height = int(self.ui.height_textbox.text())
bmi = weight/height**2
print(str(bmi)) # -> works
self.ui.bmi_label.setText(str(bmi)) # -> label does not update
window = MainWindow()
window.show()
sys.exit(app.exec_())
#Try This
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_CalcBMI(object):
def setupUi(self, CalcBMI):
CalcBMI.setObjectName(_fromUtf8("CalcBMI"))
CalcBMI.resize(400, 300)
self.pushButton = QtGui.QPushButton(CalcBMI)
self.pushButton.setGeometry(QtCore.QRect(140, 220, 111, 28))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.On_Update_Clicked)
#add textbox here to get height and weight
self.label = QtGui.QLabel(CalcBMI)
self.label.setGeometry(QtCore.QRect(150, 70, 131, 101))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(CalcBMI)
def On_Update_Clicked(self):
#Get values from textbox and calculate bmi here
bmi="23.5"
self.label.setText(bmi)
def retranslateUi(self, CalcBMI):
CalcBMI.setWindowTitle(_translate("CalcBMI", "CalcBMI", None))
self.pushButton.setText(_translate("CalcBMI", "Calculate now", None))
self.label.setText(_translate("CalcBMI", "inProgess", None))
if __name__ == "__main__":
import sys
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
app = QtGui.QApplication(sys.argv)
CalcBMI = QtGui.QWidget()
ui = Ui_CalcBMI()
ui.setupUi(CalcBMI)
CalcBMI.show()
sys.exit(app.exec_())
I have created two windows with Qt designer. Now in a third file, I'm trying to import the classes and open one of them by clicking a QPushButton in the other one.
The first one, main_gui.py, is something like this:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
...
...
...
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
The second one, single_plot_gui.py, has the same preamble and it looks like this:
class Ui_Form_single_plot(object):
def setupUi(self, Form_single_plot):
Form_single_plot.setObjectName(_fromUtf8("Form_single_plot"))
...
...
...
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form_single_plot = QtGui.QWidget()
ui = Ui_Form_single_plot()
ui.setupUi(Form_single_plot)
Form_single_plot.show()
sys.exit(app.exec_())
Now in my third file I have:
from main_gui import Ui_Form
from single_plot_gui import Ui_Form_single_plot
class SinglePlotWindow(QtGui.QMainWindow, Ui_Form_single_plot):
def __init__(self, parent=None):
super(SinglePlotWindow, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setupUi(self)
class Main_Window(QtGui.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
self.open_single_plotButton.clicked.connect(self.OpenSinglePlot)
def OpenSinglePlot(self):
window = SinglePlotWindow(self)
window.show()
But when I press the open_single_plotButton, the SinglePlotWindow appears on top of the main window, instead of showing a new window.
What am I doing wrong?
In this answer they do something very similar, but I'm not getting there...
You need to create the second window without a parent, and keep a reference to it after it's shown. So your button handler should look like this:
def OpenSinglePlot(self):
self.plotWindow = SinglePlotWindow()
self.plotWindow.show()
I am trying to build a GUI around some code that I already have. I understand how to do this when building the GUI manually, but am stuck when adding this to the python code generated by Qt Designer and pyuic. As an example, I might need a button which will allow the user to point to a file, which manually I do as such, and this works:
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn = QtGui.QPushButton('Open File', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.clicked.connect(self.loadFile)
self.setGeometry(300, 300, 250, 150)
self.show()
def loadFile(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
# some custom code for reading file and storing it
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
However, when I try to do the same in Qt Designer code the program stops before reaching the file dialog.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(130, 100, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.loadFile)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Open File", None))
def loadFile(self):
print('loadFile1')
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
print('loadFile2')
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This only prints the first statement in loadFile(), but does not open up the file dialog window. What am I doing wrong?
According to the documentation:
QString getOpenFileName (QWidget parent = None, QString caption = '',
QString directory = '', QString filter = '', Options options = 0)
QString getOpenFileName (QWidget parent = None, QString caption = '',
QString directory = '', QString filter = '', QString selectedFilter =
'', Options options = 0)
You need to pass as a parent a widget or None, in your case self is not of type object.
You must change
QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
to
QtGui.QFileDialog.getOpenFileName(None, 'Open file', '/home')
I really don't like using pyuic as long as I can avoid. There's an easier way you may try, it reduces your code. Say your UI file is called something.ui, and you have named your button in QT Designer somebutton, then the code will be:
from PyQt4 import QtCore, QtGui, uic
Ui_somewindow, _ = uic.loadUiType("something.ui") #the path to your UI
class SomeWindow(QtGui.QMainWindow, Ui_somewindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_somewindow.__init__(self)
self.setupUi(self)
self.somebutton.clicked.connect(self.loadFile)
def loadFile(self):
print('loadFile1')
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
print('loadFile2')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = SomeWindow()
window.show()
sys.exit(app.exec_())
Note that if you have QDialog replace QMainWindow with QDialog.
Hope this helps.
I am python newbie. I am developing pr-room management program.
I think if the user presses the x button , i want to give a warning message .
So, I want to use closeEvent() in first code.
In other words, first code + second code please
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\PJH11\Desktop\dia3.ui'
#
# Created: Sun Aug 17 16:23:08 2014
# by: PyQt4 UI code generator 4.11.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(190, 98)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "hi", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
Your can implement your class to create closeEvent (As your code close event), put them it and create object in main. And your can call to close event by using bool QWidget.close (self).
Full example;
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
self.Dialog = Dialog
self.Dialog.setObjectName(_fromUtf8("self.Dialog"))
self.Dialog.resize(190, 98)
self.pushButton = QtGui.QPushButton(self.Dialog)
self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(self.Dialog)
QtCore.QMetaObject.connectSlotsByName(self.Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('released()'), self.Dialog.close) # <- put signal to close when clicked.
def retranslateUi(self, Dialog):
self.Dialog.setWindowTitle(_translate("self.Dialog", "self.Dialog", None))
self.pushButton.setText(_translate("self.Dialog", "hi", None))
class QCustomDialog (QtGui.QDialog): # <- Implement your own
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QCustomDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Regards,
Okay, I really need help with this... I have a dockwidget, and in that dockwidget I have a textedit. Ok, all is fine so far, and here is the code for that:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'out/untitled.ui'
#
# Created: Mon Sep 16 19:33:15 2013
# by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(320, 240)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.dockWidget = QtGui.QDockWidget(MainWindow)
self.dockWidget.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
self.dockWidget.setObjectName(_fromUtf8("dockWidget"))
self.textEdit = QtGui.QTextEdit()
self.textEdit.setGeometry(QtCore.QRect(40, 10, 104, 71))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.dockWidget.setWidget(self.textEdit)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QMainWindow()
f = Ui_MainWindow()
f.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Now, what I want to do is place a line edit at the bottom of the window (or dockwidget) that, overlaps the text area, doesn't move when the dockwidget is resized, and fills the whole dockwidget. I have tried this:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'out/untitled.ui'
#
# Created: Mon Sep 16 19:33:15 2013
# by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(320, 240)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.dockWidget = QtGui.QDockWidget(MainWindow)
self.dockWidget.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
self.dockWidget.setObjectName(_fromUtf8("dockWidget"))
self.textEdit = QtGui.QTextEdit()
self.textEdit.setGeometry(QtCore.QRect(40, 10, 104, 71))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.dockWidget.setWidget(self.textEdit)
QtGui.QLineEdit(self.dockWidget) # Line edit
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QMainWindow()
f = Ui_MainWindow()
f.setupUi(Form)
Form.show()
sys.exit(app.exec_())
but it is not what I want. I REALLY need to get this working, so any help would be great. Thank you.
Here is an example of how to display a widget on top of another:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#---------
# IMPORT
#---------
import sys
from PyQt4 import QtGui, QtCore
#---------
# DEFINE
#---------
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.pushButtonInput = QtGui.QPushButton(self)
self.pushButtonInput.setText("Toggle the QLineEdit widget!")
self.pushButtonInput.clicked.connect(self.on_pushButtonInput_clicked)
self.textEditInput = QtGui.QTextEdit(self)
self.textEditInput.setText("This is a QTextEdit widget.")
self.textEditInput.installEventFilter(self)
self.lineEditInput = QtGui.QLineEdit(self)
self.lineEditInput.setText("This is a QLineEdit widget.")
self.lineEditInput.hide()
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.pushButtonInput)
self.layoutVertical.addWidget(self.textEditInput)
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Resize:
geometry = self.textEditInput.geometry()
self.lineEditInput.setGeometry(geometry)
return super(MyWindow, self).eventFilter(obj, event)
#QtCore.pyqtSlot()
def on_pushButtonInput_clicked(self):
if self.lineEditInput.isVisible():
self.lineEditInput.hide()
else:
self.lineEditInput.show()
#---------
# MAIN
#---------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())