AttributeError: 'class_c' object has no attribute 'lineEdit' - python

I have a gui application
I put text into text box and then click on the pushButton,
The function callprinttext() in the moduel_b.py be called.
The function callprinttext() is calling method printtext() in the moduel_c.py
but I have an error:
AttributeError: 'class_c' object has no attribute 'lineEdit'
can anyone help me with class_c?
Here's the code for that:
module_c.py
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
### I want a fix for error here
def printtext (self):
t=unicode(self.lineEdit.text())
print t
module_b.py
import sys
from GUI import Ui_MainWindow
from PyQt4 import QtCore, QtGui
class calss_b (object):
def __init__(self, parent=None):
pass
def callprinttext (self):
from module_c import class_c
instance_c=class_c()
instance_c.printtext()
main.py
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
from GUI import Ui_MainWindow
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
from module_b import calss_b
global instance_b
instance_b=calss_b(self)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), instance_b.callprinttext )
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
global myapp
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
GUI
# -*- coding: utf-8 -*-
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):
super(Ui_MainWindow, self).__init__()
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 110, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(390, 240, 151, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))

Well, one way to do it is using a call back method, this way:
In main.py
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.call_back)
def call_back(self):
txt = str(self.ui.lineEdit.text()) #lineEdit.text() returns QString Object so we need to convert it to String object
instance_b.callprinttext(txt)
Then in your calss_b method:
def callprinttext (self, txt):
from module_c import class_c
instance_c=class_c()
instance_c.printtext(txt)
And Finally in your class_c method:
def printtext (self, txt):
t=unicode(txt)
print t
EDIT :
If you don't want to add any call back method, then do it using lambda, this way:
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), \
lambda: instance_b.callprinttext(self.ui.lineEdit.text()))
And there is no need for call_back

Related

How can I use QTimer with QT' QMainWindow (QTimer works with QTWidget but not QMainWindow)

I have a QMainWindow created in QT Designer with a GraphicsView object that has been promoted to a pyQTGraph PlotWidget. I want to use a QTimer event to get live serial data (Y) and plot it with (X) being 1 second increments set by the QTimer event. However, the problem I'm having is that when using QT's QMainWindow as the main form I get the error "QObject::startTimer: QTimer can only be used with threads started with QThread", but if I use QT's QWidget as the main form everything works fine with no errors.
How can I use QTimer with QT's QMainWindow
QT4 and QT Designer
Python 3
PyQTGraph
Here is the Form Code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainPlotWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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(735, 374)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.plot = PlotWidget(self.centralwidget)
self.plot.setGeometry(QtCore.QRect(30, 40, 681, 261))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plot.sizePolicy().hasHeightForWidth())
self.plot.setSizePolicy(sizePolicy)
self.plot.setFrameShape(QtGui.QFrame.WinPanel)
self.plot.setFrameShadow(QtGui.QFrame.Sunken)
self.plot.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.plot.setObjectName(_fromUtf8("plot"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 735, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
from pyqtgraph import PlotWidget
Here is the simple test program that reproduces the error:
from PyQt4.QtCore import QThread, SIGNAL, QSettings
import sys
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from MainPlotWindow import *
'''=====================================================================
M A I N G U I
====================================================================='''
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def PlotUpdate():
print("Hello")
timer = QtCore.QTimer()
timer.timeout.connect(PlotUpdate)
timer.start(1000) # 1 Second Refesh Rate
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
Any help would be very much appreciated!
I recommend that the QTimer this inside the widget, as I show in the following example:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.PlotUpdate)
timer.start(100) # 1 Second Refesh Rate
self.curve = self.ui.plot.plot()
self.counter = 0
def PlotUpdate(self):
x = self.counter + np.arange(0, 10)
y = 10*np.sin(np.pi*x/10)
self.curve.setData(x, y)
self.counter += 1
Output:

AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'

I'm trying to connect a push-button on my main window to open a new widget when I click it. But each time I run the program I get this error:
AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'
I will be grateful if someone can help.
Here's my code:
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from gpiodialog import Ui_GPIODialog
import sys
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWidowFlags(flags)
self.ui = Ui_GPIODialog
self.ui.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
def acceptOKButtonClicked(self):
self.close()
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()
def gpioButton_clicked(self):
self.popGPIO.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainWindow = dataAcquisition()
MainWindow.show()
sys.exit(app.exec_())
mainwindow.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Tue Oct 18 02:57:57 2016
# by: PyQt4 UI code generator 4.11.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(700, 600)
MainWindow.setMinimumSize(QtCore.QSize(700, 600))
MainWindow.setMaximumSize(QtCore.QSize(700, 600))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gpioButton = QtGui.QToolButton(self.centralwidget)
self.gpioButton.setGeometry(QtCore.QRect(20, 80, 181, 61))
self.gpioButton.setObjectName(_fromUtf8("gpioButton"))
self.procButton = QtGui.QPushButton(self.centralwidget)
self.procButton.setGeometry(QtCore.QRect(20, 180, 181, 61))
self.procButton.setObjectName(_fromUtf8("procButton"))
self.startButton = QtGui.QPushButton(self.centralwidget)
self.startButton.setEnabled(True)
self.startButton.setGeometry(QtCore.QRect(20, 360, 131, 51))
self.startButton.setObjectName(_fromUtf8("startButton"))
self.stopButton = QtGui.QPushButton(self.centralwidget)
self.stopButton.setGeometry(QtCore.QRect(190, 360, 131, 51))
self.stopButton.setObjectName(_fromUtf8("stopButton"))
self.plotButton = QtGui.QPushButton(self.centralwidget)
self.plotButton.setGeometry(QtCore.QRect(20, 460, 181, 61))
self.plotButton.setObjectName(_fromUtf8("plotButton"))
self.columnView = QtGui.QColumnView(self.centralwidget)
self.columnView.setGeometry(QtCore.QRect(340, 50, 341, 251))
self.columnView.setObjectName(_fromUtf8("columnView"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(280, 20, 221, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.saveButton = QtGui.QPushButton(self.centralwidget)
self.saveButton.setEnabled(True)
self.saveButton.setGeometry(QtCore.QRect(410, 420, 211, 31))
self.saveButton.setObjectName(_fromUtf8("saveButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 27))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.gpioButton.setText(_translate("MainWindow", "GPIO Number of inputs", None))
self.procButton.setText(_translate("MainWindow", "Processing", None))
self.startButton.setText(_translate("MainWindow", "Start Acquiring", None))
self.stopButton.setText(_translate("MainWindow", "Stop Acquiring", None))
self.plotButton.setText(_translate("MainWindow", "Start Live Plot", None))
self.label_2.setText(_translate("MainWindow", "Data Acquisition System", None))
self.saveButton.setText(_translate("MainWindow", "Save this session\'s data", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
gpiodialog.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gpiodialog.ui'
#
# Created: Tue Oct 18 03:11:51 2016
# by: PyQt4 UI code generator 4.11.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_GPIODialog(object):
def setupUi(self, GPIODialog):
GPIODialog.setObjectName(_fromUtf8("GPIODialog"))
GPIODialog.resize(400, 300)
self.gpioOKButton = QtGui.QPushButton(GPIODialog)
self.gpioOKButton.setGeometry(QtCore.QRect(170, 240, 101, 31))
self.gpioOKButton.setObjectName(_fromUtf8("gpioOKButton"))
self.label = QtGui.QLabel(GPIODialog)
self.label.setGeometry(QtCore.QRect(90, 20, 191, 21))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(GPIODialog)
QtCore.QMetaObject.connectSlotsByName(GPIODialog)
def retranslateUi(self, GPIODialog):
GPIODialog.setWindowTitle(_translate("GPIODialog", "Dialog", None))
self.gpioOKButton.setText(_translate("GPIODialog", "OK", None))
self.label.setText(_translate("GPIODialog", "Choose GPIO input bins", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
GPIODialog = QtGui.QDialog()
ui = Ui_GPIODialog()
ui.setupUi(GPIODialog)
GPIODialog.show()
sys.exit(app.exec_())
You created a separate namespace for the ui, so you would need to access the button like this:
self.ui.gpioButton.clicked.connect(self.gpioButton_clicked)
However, if you'd prefer to have all the widgets as attributes of the window, do this:
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
super(gpioDialog, self).__init__(parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(dataAcquisition, self).__init__(parent)
self.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()

how to create label button in PYQT?

I need your help with my app. I undecorated python gui and now i need to create clicable label.
run.py
from PyQt4 import QtCore, QtGui
import sys
import untitled
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
def closeapp():
sys.exit(0);
untitled.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 85, 30))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(220, 100, 58, 14))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(220, 140, 58, 14))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "open", None))
self.label.setText(_translate("MainWindow", "close", None))
self.label_2.setText(_translate("MainWindow", "view", None))
Now I need to add action to label and label_2. How to make the label to perform closeapp()?
Add a function to Ui_MainWindow class (untitled.py)
def click(self,eve):
print "clicked"
Then add attribute to label object
self.label.mousePressEvent = self.click
another option is just modify run.py file like following
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def click(self,eve):
print "clicked"
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.label.mousePressEvent = self.click
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
QLabel inherits QWidget. QWidget` has an event handler mousePressEvent
so as E-ebola virus mentioned
add
self.label.mousePressEvent = self.click
and define
def click(self,event):
#do something
or
subclass QLabel
class customLabel(QLabel):
def __init__(self):
QLabel.__init__(self)
def self.mousePressEvent(self,event):
self.emit(SIGNAL("closeapp"))
emit a SIGNAL when label is pressed, i would implement it as
self.label = customLabel()
self.connect(self.label,SIGNAL('closeapp'),self.close)

PyQT: How to access MainWindow widgets from an external function?

For example, clicking button in main window calls test() function to add some text to textEdit box in main window
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'felch1.ui'
#
# Created: Fri Aug 15 15:19:31 2014
# by: PyQt4 UI code generator 4.10.4
#
# 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)
def test():
MainWindow.textEdit.append('button pushed')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 20, 131, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.textEdit = QtGui.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(30, 80, 341, 441))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), test)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "test", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I know I shouldn't noodle with the generated file, just trying to figure out how to reference main window objects from a function outside that class.
I think the easiest way would be to make test an instance method of Ui_MainWindow, and save the MainWindow object as an instance variable of Ui_MainWindow, as well.
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.main_window = MainWindow
# blah blah
self.textEdit = QtGui.QTextEdit(self)
self.pushButton = QtGui.QPushButton(self)
self.pushButton.clicked.connect(self.test)
def test(self):
self.main_window.textEdit.append('button pushed')
If you really wanted test to be a top-level function, you could also use functools.partial to pass the MainWindow object along when the test callback is executed:
from functools import partial
def test(MainWindow):
MainWindow.textEdit.append('button pushed')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
# blah blah
self.textEdit = QtGui.QTextEdit(self)
self.pushButton = QtGui.QPushButton(self)
test_func = partial(test, MainWindow) # Calling test_func(arg) actually calls test(MainWindow, arg)
self.pushButton.clicked.connect(test_func)
The possible is pass object QtGui.QMainWindow to method do your want, pass the MainWindow object to test() by using partial to pass parameter your want;
from PyQt4 import QtCore, QtGui
from functools import partial
.
.
.
def test(MainWindow):
MainWindow.textEdit.append('button pushed')
def setupUi(self, MainWindow):
.
.
.
MainWindow.textEdit = QtGui.QTextEdit(self.centralwidget) # Pass to QMainWindows not self
MainWindow.textEdit.setGeometry(QtCore.QRect(30, 80, 341, 441))
MainWindow.textEdit.setObjectName(_fromUtf8("textEdit"))
MainWindow.setCentralWidget(self.centralwidget)
.
.
.
self.retranslateUi(MainWindow)
QtCore.QObject.connect(MainWindow.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), partial(test, MainWindow))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
.
.
.
Regards,

PyQT: LineEdit over QTextEdit

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_())

Categories

Resources