How to change label text until pushbutton clicked in pyqt - python

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

Related

Clickable QLabel in PyQt? [duplicate]

This question already has answers here:
Make QLabel clickable
(4 answers)
Closed 4 years ago.
I'm creating a side menu using pyqt4.
I turned the QLabel (burgermenu) into a picture of a burger menu, and I'm having trouble making it clickable.
My intension is that when I click the burgermenu image, the inner frame (innerframe) hides totally, and when I click burgermenu again; the innerframe appears with its content.
How can I achieve that?
Here is my code:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(479, 381)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.outerframe = QtGui.QFrame(self.centralwidget)
self.outerframe.setGeometry(QtCore.QRect(190, 80, 151, 211))
self.outerframe.setFrameShape(QtGui.QFrame.StyledPanel)
self.outerframe.setFrameShadow(QtGui.QFrame.Raised)
self.outerframe.setObjectName(_fromUtf8("outerframe"))
self.burgermenu = QtGui.QLabel(self.outerframe)
self.burgermenu.setGeometry(QtCore.QRect(70, 10, 71, 20))
self.burgermenu.setObjectName(_fromUtf8("burgermenu"))
self.innerframe = QtGui.QFrame(self.outerframe)
self.innerframe.setGeometry(QtCore.QRect(10, 60, 131, 141))
self.innerframe.setFrameShape(QtGui.QFrame.StyledPanel)
self.innerframe.setFrameShadow(QtGui.QFrame.Raised)
self.innerframe.setObjectName(_fromUtf8("innerframe"))
self.widget = QtGui.QWidget(self.innerframe)
self.widget.setGeometry(QtCore.QRect(20, 30, 91, 99))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.verticalLayout.addWidget(self.label_2)
self.dateEdit = QtGui.QDateEdit(self.widget)
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.verticalLayout.addWidget(self.dateEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 479, 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))
self.burgermenu.setText(_translate("MainWindow", "burgermenu", None))
self.burgermenu.setPixmap(QPixmap("/Users/Desktop/menu.png"))
self.label_2.setText(_translate("MainWindow", "Date:", 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_())
Here you can use method name mousePressEvent
self.burgermenu.mousePressEvent = self.dosomething
after that in dosomething method you can call method called hide or show for innerframe.
self.innerframe.hide()
or
self.innerframe.show()
and dont forget to add event as parameter in dosomething
you can overwrite the mousePressEvent of the QLabel like:
def setupClickToggle(self):
def mousePressEvent(*args, **kwargs):
self.innerframe.setVisible(not self.innerframe.isVisible())
self.burgermenu.mousePressEvent = mousePressEvent
and you simply call it once in your init or during startup:
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
setupClickToggle(MainWindow)
MainWindow.show()

How to connect a QPushButton to a function

I want to create a simple program with a PyQt GUI. For this I used Qt Designer, and I made a window with a push-button named OpenImage. Moreover, I have a file named rony.py that has a function show. So I want that when the user clicks the button, the image will appear.
This is my code:
from PyQt4 import QtCore, QtGui
import rony
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.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.OpenImage = QtGui.QPushButton(self.centralwidget)
self.OpenImage.setObjectName(_fromUtf8("OpenImage"))
self.horizontalLayout.addWidget(self.OpenImage)
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.OpenImage.setText(_translate("MainWindow", "PushButton", None))
OpenImage.connect(show()) # Why this line don't work
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_())
rony.py
from PIL import Image
img = Image.open("pipe.png")
def show():
img.show()
You have to reference the name of your script when calling a function from it. It works as a library:
OpenImage.connect(rony.show())

How to refresh items in Qcombobox [python]?

I have a list of data which is shown in the Qcombobox (using 'addItems')
Now - if the list is changed by loading data from a file with pushbutton - i don't see the new data in the combobx.
The new data is there (I can print it after loading)
What do I miss?
below is the code for the simplified gui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
import pandas as pd
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 __init__(self):
self.comboData=['None']
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(456, 172)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 60, 69, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.setEditable(True)
self.comboBox.addItems(self.comboData)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 60, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.load)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 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", "Load", None))
def load(self):
fileName=QFileDialog.getOpenFileName(MainWindow,'Load File')
data_pd=pd.read_csv(fileName,index_col=0,na_filter=False)
self.comboData=list(data_pd.var1)
print(self.comboData)
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_())
you only load the data to self.comboData but do not load them to self.comboBox.
At the end of
def load(self):
add the following lines:
self.comboBox.clear() # delete all items from comboBox
self.comboBox.addItems(self.comboData) # add the actual content of self.comboData
As the model used by QComboBox emits the dataChanged() signal whenever the data in an item are changed, the combobox is repainted automatically and it is not necessary to update the combobox by
self.comboBox.update()

The Code does not work successfully when the function is called

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

TypeError: changeText() takes exactly 2 arguments (0 given)

Im using python (2.7.5) and pyqt (4.8.4) and i want to create a simple gui programm that displays value of the function (increment) in a "lineEdit" object everytime the button is clicked.
My code:
z=0
def myfunc1():
global z
z=z+1
print (z)
def changeText(self, event):
lineEdit.setText(str(z))
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(613, 545)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(244, 352, 111, 51))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(242, 290, 111, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), myfunc1)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), changeText)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "click me", None))
self.lineEdit.setText(_translate("Form", "functionvalue", 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_())
Programm begins to work, but after clicking the button it gives an error message:
TypeError: changeText() takes exactly 2 arguments (0 given)
As far as i understand, i should somehow define those 2 arguments: (self, event)..But how?
Here is a working code:
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("Dialog"))
Form.resize(151, 67)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout.addWidget(self.pushButton)
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.changeText)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Click me", None))
self.lineEdit.setPlaceholderText(_translate("Form", "functionvalue", None))
class MyForm(QtGui.QDialog, Ui_Form):
def __init__(self, z=0):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.z = z
def myfunc1(self):
self.z+=1
print (self.z)
def changeText(self):
self.myfunc1()
self.lineEdit.setText(str(self.z))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = MyForm()
Form.show()
sys.exit(app.exec_())
you gave changetext() the self argument even though it isn't in the class UI_form so it doesnt need the self
then when you called it you didn't give any arguments with it on this line:
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), changeText)
and its expecting to parameters to be called with it becuase when it was defined you gave it a self and an event argument

Categories