Changing Pixmap image automatically at runtime in PyQt - python

I wish to change the image of a pixmap being displayed in a window automatically using a while loop that has a defined delay and uses locations of images stored in a string list.
All possibilities I tried led to the first image being displayed but not changing at runtime.
Any changes gets reflected only after the program has completed all kinds of execution.
from PyQt4 import QtCore, QtGui
import time
import sys
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(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(598, 555)
self.horizontalLayout = QtGui.QHBoxLayout(Form)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.start_pulse = QtGui.QPushButton(Form)
self.start_pulse.setObjectName(_fromUtf8("start_pulse"))
self.horizontalLayout.addWidget(self.start_pulse)
self.label = QtGui.QLabel(Form)
self.label.setAutoFillBackground(True)
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI.png")))
self.label.setScaledContents(True)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.start_pulse.setText(_translate("Form", "start", None))
self.start_pulse.clicked.connect(self.soumyajit)
def soumyajit(self):
j=0
while(1):
print("change")
if j==0:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI2.png")))
if j==1:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI.png")))
if j==2:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI2.png")))
if j==3:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI.png")))
j=j+1
#delay
time.sleep(.5)
if __name__=='__main__':
app=QtGui.QApplication(sys.argv)
ex = Ui_Form()
ex.show()
sys.exit(app.exec_())

Following code what I modified from your code, it works well:
Original Code from Changing Pixmap image automatically at runtime in PyQt
Modified for solving problem by WonJong Kim (wjkim#etri.re.kr)
import sys
import time
import numpy
from PyQt4 import QtCore, QtGui
import PyQt4.Qwt5 as Qwt
numPoints=1000
xs=numpy.arange(numPoints)
ys=numpy.sin(3.14159*xs*10/numPoints) #this is our data
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(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.index = 0
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(500, 300)
self.horizontalLayout = QtGui.QHBoxLayout(Form)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.start_pulse = QtGui.QPushButton(Form)
self.start_pulse.setObjectName(_fromUtf8("start_pulse"))
self.stop_pulse = QtGui.QPushButton(Form)
self.stop_pulse.setObjectName(_fromUtf8("stop_pulse"))
self.horizontalLayout.addWidget(self.start_pulse)
self.horizontalLayout.addWidget(self.stop_pulse)
self.label = QtGui.QLabel(Form)
self.label.setAutoFillBackground(True)
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("GUI.png")))
self.label.setScaledContents(True)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def refreshUi(self):
if self.index%10==0:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/10.jpg")))
if self.index%10==1:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/1.jpg")))
if self.index%10==2:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/2.jpg")))
if self.index%10==3:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/3.jpg")))
if self.index%10==4:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/4.jpg")))
if self.index%10==5:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/5.jpg")))
if self.index%10==6:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/6.jpg")))
if self.index%10==7:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/7.jpg")))
if self.index%10==8:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/8.jpg")))
if self.index%10==9:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/9.jpg")))
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.start_pulse.setText(_translate("Form", "start", None))
self.start_pulse.clicked.connect(self.start)
self.stop_pulse.setText(_translate("Form", "stop", None))
self.stop_pulse.clicked.connect(self.stop)
def start(self):
self.timer1.start(500.0) #set the interval (in ms)
def stop(self):
self.timer1.stop() #stop the timer
def change_index(self):
self.index += 1
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
win_plot = Ui_Form()
win_plot.index = 0
win_plot.timer = QtCore.QTimer() #start a timer (to call replot events)
win_plot.timer.start(100.0) #set the interval (in ms)
win_plot.connect(win_plot.timer, QtCore.SIGNAL('timeout()'), win_plot.refreshUi)
win_plot.timer1 = QtCore.QTimer() #start a timer (to call replot events)
win_plot.connect(win_plot.timer1, QtCore.SIGNAL('timeout()'),
win_plot.change_index)
# show the main window
win_plot.show()
sys.exit(app.exec_())

You run your loop in GUI thread so it's freezing your app. It is a bad approach. You should use QTimer instead. Use timeout() signal to do all your image changes inside special slot . QTimer is asynchronous so it will not freeze your app.
You can also call processEvents() inside loop but it is not good solution, timer is better than loop with sleep.

Related

Real Time Loop (Clock) using Date Edit in PYQT4

My question is this: How do I use the qtimeEdit in PYQT4 to loop & update, so it is always displaying the current time. Essentially, I would like it to function just like the Windows Taskbar time and date.
For example:
from PyQt4 import QtCore, QtGui
from PyQt4 import phonon
import sys
import os
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.setWindowFlags(QtCore.Qt.WindowContextHelpButtonHint)
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(885, 450)
MainWindow.setMinimumSize(QtCore.QSize(885, 450))
MainWindow.setMaximumSize(QtCore.QSize(885, 491))
MainWindow.setMouseTracking(False) self.timeEdit = QtGui.QTimeEdit(self.splitter)
self.timeEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.timeEdit.setAutoFillBackground(True)
self.timeEdit.setWrapping(True)
self.timeEdit.setFrame(True)
self.timeEdit.setAlignment(QtCore.Qt.AlignCenter)
self.timeEdit.setReadOnly(True)
self.timeEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.timeEdit.setKeyboardTracking(True)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
def retranslateUi(self, MainWindow):
Today = QtCore.QDate.currentDate()
self.dateEdit.setDate(Today)
self.ui.timeEdit = QtGui.QTimeEdit(self)
self.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.timeEdit.setTime(current.time())
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
def VoluemChanger(self, audioOutput):
self.ui.volumeSlider.setAudioOutput(self.ui.videoPlayer.audioOutput())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
Try this code, implement a timer that will be triggered every second, at which point the time is read and updated.
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
# Uncomment if you want to change the language
# self.setLocale(QtCore.QLocale(QtCore.QLocale.Spanish, QtCore.QLocale.Peru))
self.verticalLayout = QtGui.QVBoxLayout(self)
self.dateEdit = QtGui.QDateEdit(self)
self.dateEdit.setDisplayFormat("MMM dd yyyy")
self.verticalLayout.addWidget(self.dateEdit)
self.timeEdit = QtGui.QTimeEdit(self)
self.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.verticalLayout.addWidget(self.timeEdit)
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.dateEdit.setDate(current.date())
self.timeEdit.setTime(current.time())
def main():
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Output:
if case of #JeremyStiefel:
# -*- 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!
import sys
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(113, 120)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.timeEdit = QtGui.QTimeEdit(self.centralwidget)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
self.timeEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.timeEdit.setAutoFillBackground(True)
self.timeEdit.setWrapping(True)
self.timeEdit.setFrame(True)
self.timeEdit.setAlignment(QtCore.Qt.AlignCenter)
self.timeEdit.setReadOnly(True)
self.timeEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.timeEdit.setKeyboardTracking(True)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
self.verticalLayout.addWidget(self.timeEdit)
self.dateEdit = QtGui.QDateEdit(self.centralwidget)
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.dateEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.dateEdit.setAutoFillBackground(True)
self.dateEdit.setWrapping(True)
self.dateEdit.setFrame(True)
self.dateEdit.setAlignment(QtCore.Qt.AlignCenter)
self.dateEdit.setReadOnly(True)
self.dateEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.dateEdit.setKeyboardTracking(True)
self.dateEdit.setObjectName(_fromUtf8("timeEdit"))
self.verticalLayout.addWidget(self.dateEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 113, 22))
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))
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self):
super(ControlMainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Uncomment if you want to change the language
# self.setLocale(QtCore.QLocale(QtCore.QLocale.Spanish, QtCore.QLocale.Peru))
self.ui.dateEdit.setDisplayFormat("MMM dd yyyy")
self.ui.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.ui.dateEdit.setDate(current.date())
self.ui.timeEdit.setTime(current.time())
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if result == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main():
app = QtGui.QApplication(sys.argv)
w = ControlMainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Output:
Remove
def VoluemChanger(self, audioOutput):
self.ui.volumeSlider.setAudioOutput(self.ui.videoPlayer.audioOutput())
Because I did not find the definition of the variables in your code

python qt designer mouse tracking cursor position

I am making a GUI for touchscreen
Pushbuttons in this GUI should generate signal when mouse cursor is on the pushbuttons
But qt designer does not have that kind of signal (I already tried released, clicked, pressed)
Therefore, I think constantly tracking cursor position can be a solution
However, I have no idea how to implement mouse tracking (such as mousemoveEvent) in the code generated by qt designer and pyuic
If I use the mouse tracking code from other examples, it does not work...
Please help me
Here is the code what contains essential parts only
from PyQt4 import QtCore, QtGui
import sys
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 mouseMoveEvent(self, event):
current = QtGui.QCursor.pos()
x = current.x()
y = current.y()
print("Mouse %d %d" % (x,y))
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1920, 720)
import resources_rc
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setMouseTracking(True)
MainWindow.show()
sys.exit(app.exec_())
I solved this problem
I used a code from https://github.com/bkach/earthquakeviz/blob/master/pyqt.py
Making one more class is working
Complete code
from PyQt4 import QtCore, QtGui
import sys
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(1920, 720)
MainWindow.setMouseTracking(True)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setMouseTracking(True)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
class MainWIndowTest(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.centralwidget.installEventFilter(self)
def eventFilter(self, object, event):
if (event.type() == QtCore.QEvent.MouseMove):
pos = event.pos()
print("%d, %d" % (pos.x(), pos.y()))
return QtGui.QWidget.eventFilter(self, object, event)
def mouseMoveEvent(self, event):
print("Moved")
import resources_rc
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
win = MainWIndowTest()
win.show()
sys.exit(app.exec_())

Pyqt4 QFileDialog Constructor: what to use as parent parameter?

I'm using pyqt4 and python 2.7.11. I have a problem with the file/directory dialogs: I don't know what to use as the parent parameter.
If I use "self" or "None", PyCharm tells me that it's expecting "QFileDialog", and it got "AppTest" and "None" instead, respectively.
If I use "parent=self" or "parent=None", PyCharm tells me that the "self" parameter is unfilled.
Please see the example code below; I'm using Qt Designer to create the ui, let me know if you need that code as well.
import sys
from PyQt4 import QtGui
import ui_test as main_frame
class AppTest(QtGui.QMainWindow, main_frame.Ui_MainAppWindow):
def __init__(self, parent=None):
super(AppTest, self).__init__(parent)
self.setupUi(self)
self.pushButton1.clicked.connect(self.dialog1)
self.show()
def dialog1(self):
file1 = str(QtGui.QFileDialog.getOpenFileName(
parent=self,
caption="Locate file 1",
directory="",
filter="Graphics files (*.jpg *.jpeg)"))
print file1
def main():
app = QtGui.QApplication(sys.argv)
gui = AppTest()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
EDIT: Here's the code for the ui:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test_ui.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_MainAppWindow(object):
def setupUi(self, MainAppWindow):
MainAppWindow.setObjectName(_fromUtf8("MainAppWindow"))
MainAppWindow.resize(269, 195)
self.centralwidget = QtGui.QWidget(MainAppWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.pushButton1 = QtGui.QPushButton(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton1.sizePolicy().hasHeightForWidth())
self.pushButton1.setSizePolicy(sizePolicy)
self.pushButton1.setObjectName(_fromUtf8("pushButton1"))
self.verticalLayout.addWidget(self.pushButton1)
self.pushButton2 = QtGui.QPushButton(self.centralwidget)
self.pushButton2.setObjectName(_fromUtf8("pushButton2"))
self.verticalLayout.addWidget(self.pushButton2)
MainAppWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainAppWindow)
QtCore.QMetaObject.connectSlotsByName(MainAppWindow)
def retranslateUi(self, MainAppWindow):
MainAppWindow.setWindowTitle(_translate("MainAppWindow", "MainWindow", None))
self.pushButton1.setText(_translate("MainAppWindow", "PushButton1", None))
self.pushButton2.setText(_translate("MainAppWindow", "PushButton2", None))

AttributeError: 'Ui_Form' object has no attribute 'printHam_btn'

I'm started learning QtDesigner (Python) with Qt 4.8.6 and I follow this tutorial:
https://www.youtube.com/watch?v=GLqrzLIIW2E
but it showing me sometimes error in title sometimes AttributeError: 'Ui_Form' object has no attribute 'printHam_btn'. Can someone please tell me what i have to do or fix my code.
Thanks!
I know that this problem is already post in this forum but I can't find out what I have to do in my case.
CODE:
from PyQt4 import QtCore, QtGui
import sys
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(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.verticalLayout_2 = QtGui.QVBoxLayout(Form)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.print_ham = QtGui.QPushButton(Form)
self.print_ham.setObjectName(_fromUtf8("print_ham"))
self.verticalLayout.addWidget(self.print_ham)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Super ham", None))
self.print_ham.setText(_translate("Form", "print ham", None))
self.printHam_btn.clicked.connect(self.printHam)
def printHam(self):
print('Ham!')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_window = Ui_Form()
main_window.show()
sys.exit(app.exec_())
The issue is here:
self.printHam_btn.clicked.connect(self.printHam)
You call your QPushButton instance differently, so you would need to change this line to:
self.print_ham.clicked.connect(self.printHam)

Using simple python script in qt (via QT-designer)

I am a python and qt-designer newbie user. I have made a simple python code for random generating numbers. Now what i want is when i click a button the output will be on GUI and not on terminal. So far i have managed to click a button and the output will be generated in terminal but that is not what i want.
So here is the qt GUI which i have made in QT-designer with no modification as to script. Thanks for any help!
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qt_hello.ui'
#
# Created: Sat Jul 20 21:22:41 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_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(210, 230, 95, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 30, 121, 71))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.label.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "PushButton", None))
self.label.setText(_translate("Form", "TextLabel", None))
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_())
I think I understand your question, but I'm not entirely certain. Anyway, I think what you want to do is something like this. First, instead of clearing the label, replace it with something like:
QtCore.QObject.connect([...], self.button_clicked)
Now, you need to create that function in your class:
def button_clicked(self):
x = 100 # Generate your number here
self.label.setText("My number: {0}".format(x))

Categories