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
Related
i have two windows,i want to hide it after three seconds by using Qt timer,but its overlapping...its probably occurs when window size sets to
"showMaximized"
from PyQt4 import QtCore, QtGui
from PyQt4 import QtGui
from PyQt4.QtCore import Qt, QPoint
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.showMaximized()
Form.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(450, 317, 300, 61))
self.label.setStyleSheet(_fromUtf8("font: 75 60pt \"Tlwg Mono\";color: rgb(255, 255, 255);"))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "omniOS", None))
class Ui_Dialog1(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.showMaximized()
Dialog.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.centralwidget = QtGui.QWidget(Dialog)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
class Dialog(QtGui.QDialog, Ui_Form):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
class Dialog1(QtGui.QDialog, Ui_Dialog1):
def __init__(self, parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(QtGui.QPen(QtCore.Qt.white))
painter.drawArc(QtCore.QRectF(640, 330, 35, 35), 0, 5750)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w1=Dialog()
w2=Dialog1()
def on_timeout():
w1.hide()
w2.show()
QtCore.QTimer.singleShot(3000, on_timeout)
sys.exit(app.exec_())
what I need to do is, I need to get
second window after three seconds when
size sets to maximized.
Form.showMaximized()
This change to (form.resize) its working what i expected.
Any help plz
You do not observe that w1 is closed because w2 is above w1, but if it is working, so you point out in the comments I understand that you want initially only visible w1 and after 3 seconds w2 is displayed and w1 is hidden, considering that the solution is the following:
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w1=Dialog()
w2=Dialog1()
w2.hide()
QtCore.QTimer.singleShot(3000, w1.hide)
QtCore.QTimer.singleShot(3000, w2.showMaximized)
sys.exit(app.exec_())
I've created a MainWindow design called Ui_Dashboard within Qt Designer. I've also created a widget called "units_table", which I'd like to import and display within the Ui_Dashboard.
I've created a new class and inherited the Ui_Dashboard class, but for some reason cannot access the verticalLayout object. See code below:
dashboard.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dashboard.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_Dashboard(object):
def setupUi(self, Dashboard):
Dashboard.setObjectName(_fromUtf8("Dashboard"))
Dashboard.resize(800, 600)
self.centralwidget = QtGui.QWidget(Dashboard)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(50, 21, 683, 360))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.units_button = QtGui.QPushButton(self.widget)
self.units_button.setCheckable(True)
self.units_button.setChecked(False)
self.units_button.setObjectName(_fromUtf8("units_button"))
self.horizontalLayout.addWidget(self.units_button)
self.calls_button = QtGui.QPushButton(self.widget)
self.calls_button.setCheckable(True)
self.calls_button.setObjectName(_fromUtf8("calls_button"))
self.horizontalLayout.addWidget(self.calls_button)
self.vehicles_button = QtGui.QPushButton(self.widget)
self.vehicles_button.setCheckable(True)
self.vehicles_button.setObjectName(_fromUtf8("vehicles_button"))
self.horizontalLayout.addWidget(self.vehicles_button)
self.persons_button = QtGui.QPushButton(self.widget)
self.persons_button.setCheckable(True)
self.persons_button.setObjectName(_fromUtf8("persons_button"))
self.horizontalLayout.addWidget(self.persons_button)
self.pushButton_6 = QtGui.QPushButton(self.widget)
self.pushButton_6.setCheckable(True)
self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
self.horizontalLayout.addWidget(self.pushButton_6)
self.pushButton_4 = QtGui.QPushButton(self.widget)
self.pushButton_4.setCheckable(True)
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.horizontalLayout.addWidget(self.pushButton_4)
self.pushButton_5 = QtGui.QPushButton(self.widget)
self.pushButton_5.setCheckable(True)
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.horizontalLayout.addWidget(self.pushButton_5)
self.verticalLayout.addLayout(self.horizontalLayout)
self.inserted_module = QtGui.QWidget(self.widget)
self.inserted_module.setMinimumSize(QtCore.QSize(481, 321))
self.inserted_module.setObjectName(_fromUtf8("inserted_module"))
self.verticalLayout.addWidget(self.inserted_module)
Dashboard.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(Dashboard)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
Dashboard.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(Dashboard)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
Dashboard.setStatusBar(self.statusbar)
self.retranslateUi(Dashboard)
QtCore.QMetaObject.connectSlotsByName(Dashboard)
def retranslateUi(self, Dashboard):
Dashboard.setWindowTitle(_translate("Dashboard", "MainWindow", None))
self.units_button.setText(_translate("Dashboard", "Units", None))
self.calls_button.setText(_translate("Dashboard", "Calls", None))
self.vehicles_button.setText(_translate("Dashboard", "Vehicles", None))
self.persons_button.setText(_translate("Dashboard", "Persons", None))
self.pushButton_6.setText(_translate("Dashboard", "PushButton", None))
self.pushButton_4.setText(_translate("Dashboard", "PushButton", None))
self.pushButton_5.setText(_translate("Dashboard", "PushButton", None))
main.py
import sys
from PyQt4 import QtCore, QtGui, uic
from dashboard import Ui_Dashboard
class MainWindow(QtGui.QMainWindow, Ui_Dashboard):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.units_table = uic.loadUi('units_table.ui', self)
self.inserted_module = self.units_table
self.inserted_module.setMinimumSize(QtCore.QSize(481, 321))
self.verticalLayout.addWidget(self.inserted_module)
self.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
I'm able to display the imported widget perfectly fine, but I need to add it as a child to the vertical layout, which is where I'm having difficulties. Despite Ui_Dashboard being inherited, I still can't access the verticalLayout.
I keep getting the following error:
AttributeError: 'MainWindow' object has no attribute 'verticalLayout'
verticalLayout does not exist because it is created in the setupUi() method, what you must do is load it before you want to use it. Another mistake is that you should not pass self to loadUi(), since what you will do is reimplement the widget's design:
import sys
from PyQt4 import QtCore, QtGui, uic
from dashboard import Ui_Dashboard
class MainWindow(QtGui.QMainWindow, Ui_Dashboard):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.units_table = uic.loadUi('units_table.ui')
self.verticalLayout.addWidget(self.units_table )
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
how do i change the text of a label until a push button is clicked in pyqt (how do i keep this in loop)
here is the code i tried
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(376, 290)
MainWindow.setMinimumSize(QtCore.QSize(376, 290))
MainWindow.setMaximumSize(QtCore.QSize(376, 290))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(150, 210, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 70, 91, 31))
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.i = 1
while self.pushButton.clicked:
self.count()
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def count(self):
self.i+=1
print self.i
self.label.setText(str(self.i))
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
self.label.setText(_translate("MainWindow", "TextLabel", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication([])
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I tried this code but the GUI doesn't show up and goes to a infinite loop.so, how do i loop this code properly
this is the loop part and function i kept
self.i = 1
while self.pushButton.clicked:
self.count()
def count(self):
self.i+=1
print self.i
self.label.setText(str(self.i))
thanks in advance
raajvamsy
The GUI is not friendly with loops since they have the default main loop: app.exec_(), the most recommended in your case is to use a QTimer that starts as soon as the application is shown and stops when you press the button.
I always recommend not to modify the code that generates Qt Designer, it is better to create a new class that implements the logic and use the generated view, all the above I implemented it in the following code:
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
counter = 0
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.onTimeout)
timer.start(10) # 10 milliseconds
self.pushButton.clicked.connect(timer.stop)
def onTimeout(self):
self.counter += 1
self.label.setText(str(self.counter))
I want to call method (close_ok()) in (main.py ) from method :close_call() in (module_b.py), but the Code does not work successfully when the function is called.
can anyone help me with this problem?
Here's the code for that :
###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_c import class_c
global b
b=class_c()
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )
def close_ok(self):
##But it can not be done successfully.
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
global myapp
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
### module_b.py
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
### I want call method (close_ok()) in (menu class) from here
def close_call (self):
from main import MainWindow
t=MainWindow()
t.close_ok()
###GUIpy
# -*- 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"))
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))
you use diffrent instances of the class MainWindow() so it wont close the original window, change it like this:
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_c import class_c
global b
b=class_c(self) #NOTICE THE CHANGE HERE
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )
def close_ok(self):
self.close()
and change module b to do:
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
def close_call (self):
from main import MainWindow
self.parent.close_ok()# NOTICE THIS CHANGE, parent is now the original MainWindow
this way you pass your original MainWindow instancce to class_c and use it, instead of creating a new instance
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.