Qt GUI clickable Qframe or QWidget or Groupbox - python

I have a problem with my code :/
My program needs to click a QFrame, QWidget or QGroupBox.
At the moment I use mouseReleaseEvent but it only works when my function doesn't have values set.
def testc(self,e):
print(e)
#when I use this code:
self.frame.mousePressEvent = self.testc;
#it's okey
#but when I'm using
self.frame.mousePressEvent = self.testc("ssssss");
# after starting the function automatically performs a click not working
How can I make a clicking widget, frame or group-box?
My qt code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ar.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_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(571, 149)
self.frame = QtGui.QFrame(Form)
self.frame.setGeometry(QtCore.QRect(30, 30, 120, 80))
self.frame.setStyleSheet(_fromUtf8("background-color: rgb(88, 169, 255);"))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(17, 23, 91, 31))
self.label.setObjectName(_fromUtf8("label"))
self.widget = QtGui.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(210, 30, 120, 80))
self.widget.setStyleSheet(_fromUtf8("background-color: rgb(255, 158, 160);"))
self.widget.setObjectName(_fromUtf8("widget"))
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setGeometry(QtCore.QRect(30, 40, 81, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.groupBox = QtGui.QGroupBox(Form)
self.groupBox.setGeometry(QtCore.QRect(380, 30, 120, 80))
self.groupBox.setStyleSheet(_fromUtf8("background-color: rgb(56, 30, 255);"))
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.label_3 = QtGui.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(7, 34, 101, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "clicable frame", None))
self.label_2.setText(_translate("Form", "click widget", None))
self.groupBox.setTitle(_translate("Form", "GroupBox", None))
self.label_3.setText(_translate("Form", "click groupbox", None))
The point is that I want a group of objects in my QFrame. After clicking the QFrame I would like my program to write, after clicking.

Create a function that does not take arguments
You have to assign a function to mousePressEvent and self.testc("ssssss") does (probably) not return a function.
What you can do is to create another function
def f(self):
return self.testc("ssssss")
and assign
self.frame.mousePressEvent = self.f
For such one-liners it is often preferred to create lambda
self.frame.mousePressEvent = lambda: self.tests("ssssss")

Related

Pyqt4 age calculator does not output on label

My python/QT (Pyqt4) program is supposed to output a person's age after the calculate button has been pressed, but it doesnt, where have I gone wrong. My code and screenshot of the program when running is hereby attached.
my python code that does not display the text in a label.
this is my .pyw file
import sys
from ass2q2 import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.calculateBtn, QtCore.SIGNAL('clicked()'),self.dispmessage)
def dispmessage(self):
bd=(self.ui.calBD.selectedDate())
curr= QDate.currentDate()
yourAge = bd.daysTo(curr)
self.ui.labelOutput.setText("Your Age (in days) is: " +yourAge.toString())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
ass2q2.py file
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ass2q2.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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(705, 531)
self.calBD = QtGui.QCalendarWidget(Dialog)
self.calBD.setGeometry(QtCore.QRect(40, 150, 280, 155))
self.calBD.setObjectName(_fromUtf8("calBD"))
self.calCurr = QtGui.QCalendarWidget(Dialog)
self.calCurr.setEnabled(False)
self.calCurr.setGeometry(QtCore.QRect(370, 150, 280, 155))
self.calCurr.setObjectName(_fromUtf8("calCurr"))
self.labelbirth = QtGui.QLabel(Dialog)
self.labelbirth.setGeometry(QtCore.QRect(40, 120, 271, 16))
self.labelbirth.setObjectName(_fromUtf8("labelbirth"))
self.labelToDate = QtGui.QLabel(Dialog)
self.labelToDate.setGeometry(QtCore.QRect(380, 120, 261, 21))
self.labelToDate.setObjectName(_fromUtf8("labelToDate"))
self.labelOutput = QtGui.QLabel(Dialog)
self.labelOutput.setGeometry(QtCore.QRect(60, 340, 581, 41))
self.labelOutput.setLineWidth(3)
self.labelOutput.setText(_fromUtf8(""))
self.labelOutput.setObjectName(_fromUtf8("labelOutput"))
self.calculateBtn = QtGui.QPushButton(Dialog)
self.calculateBtn.setGeometry(QtCore.QRect(290, 460, 141, 51))
self.calculateBtn.setObjectName(_fromUtf8("calculateBtn"))
self.labelApp = QtGui.QLabel(Dialog)
self.labelApp.setGeometry(QtCore.QRect(140, 60, 411, 41))
self.labelApp.setObjectName(_fromUtf8("labelApp"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.labelbirth.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600;\">Select the Birth Date</span></p></body></html>", None))
self.labelToDate.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600;\">Current Date</span></p></body></html>", None))
self.calculateBtn.setText(_translate("Dialog", "C A L C U L A T E", None))
self.labelApp.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">AGE (in days) CALCULATOR</span></p></body></html>", None))

hide a frame after 3 seconds/ by calling a function (need to done in both way)

I create 2 frames in the same window with some attributes in it. now am able to call it separately or can display both
from PyQt4 import QtCore, QtGui
import time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def frame1(self,Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
def frame2(self,Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
self.frame1(Dialog)
self.frame2(Dialog)
if __name__=="__main__":
ui=Ui_Dialog()
import sys
a = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
ui.setupUi(w)
w.show()
sys.exit(a.exec_())
what I need to do is, I need to get the first window after a 3 second I need to get the second window + hide the first one without overlapping it
self.frame1(Dialog) # call this when the program start
self.frame2(Dialog) # call this after 3 second self.frame1(Dialog)
Any help would be appreciated ThankYou
That a code works does not imply that it is correct, for example I see that you have combined 2 codes generated by Qt Designer and obviously they are going to overlap, the correct thing is to have 2 windows that are exchanged as shown below using QTimer.
from PyQt4 import QtCore, QtGui
import time
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_Dialog1(object):
def setupUi(self, Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog2(object):
def setupUi(self, Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Dialog1(QtGui.QDialog, Ui_Dialog1):
def __init__(self, parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
class Dialog2(QtGui.QDialog, Ui_Dialog2):
def __init__(self, parent=None):
super(Dialog2, self).__init__(parent)
self.setupUi(self)
if __name__=="__main__":
import sys
a = QtGui.QApplication(sys.argv)
w1 = Dialog1()
w2 = Dialog2()
def on_timeout():
w1.hide()
w2.show()
w1.show()
QtCore.QTimer.singleShot(3000, on_timeout)
sys.exit(a.exec_())

Python application that adds an item to a List Widget, and edit and delete an item from it

I have been struggling with this for quite sometime, to add male or female to either listbox1 or 2 is working like a charm, when I try to delete it does work but when I select one of the items in lets say listbox one and I go to listbox two it still keeps the selection I made active in listbox one thus when I click delete Iam deleting both the highlighted items in listbox1 and listbox2, now where iam struggling is the edit part iam not sure how to do this but let me explain I want to tell it that when listbox1 is selected edit only the selected item whether its in listbox1 or 2 but for some reading it only changes my row two of listbox2 when I try to make edit and it then also adds it to listbox1 here is my current script code:
import sys
from addlist import *
from PyQt4.QtGui import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.btnAddName, QtCore.SIGNAL('clicked()'), self.addtolist)#calls the addtolist function
QtCore.QObject.connect(self.ui.btnDelete, QtCore.SIGNAL('clicked()'), self.delete)#calls the delete function
QtCore.QObject.connect(self.ui.btnEdit, QtCore.SIGNAL('clicked()'), self.edit)#calls the edit function
self.ui.rbMale.setChecked(1) #radio button rbMale checked by default when application loads
#conditions whether name should be added to the Girls List or Boys List
def addtolist(self):
if self.ui.rbMale.isChecked()==True:
self.ui.lstBoys.addItem(self.ui.edtName.text())
self.ui.edtName.setText('')
self.ui.edtName.setFocus()
elif self.ui.rbFemale.isChecked()==True:
self.ui.lstGirls.addItem(self.ui.edtName.text())
self.ui.edtName.setText('')
self.ui.edtName.setFocus()
#conditions whether name in Girls List or Boys List should be deleted
def delete(self):
self.ui.lstBoys.currentRow()
self.ui.lstGirls.currentRow()
self.ui.lstBoys.takeItem(self.ui.lstBoys.currentRow())
self.ui.lstGirls.takeItem(self.ui.lstGirls.currentRow())
def edit(self):
#item1=self.ui.lstBoys.currentRow()
#item2=self.ui.lstGirls.currentRow()
if self.ui.lstBoys.currentRow():
newtext, ok=QInputDialog.getText(self, "Enter new Name", "Enter new Name")
if ok and (len(newtext)!=0):
self.ui.lstBoys.takeItem(self.ui.lstBoys.currentRow())
self.ui.lstBoys.insertItem(self.ui.lstBoys.currentRow(), QListWidgetItem(newtext))
else:
newtext1, oke=QInputDialog.getText(self, "Enter new Name", "Enter new Name")
if oke and (len(newtext1)!=0):
self.ui.lstGirls.takeItem(self.ui.lstGirls.currentRow())
self.ui.lstGirls.insertItem(self.ui.lstGirls.currentRow(), QListWidgetItem(newtext1))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
Assistance will be much appreciated.
Thank you
As requested please see below:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'addlist.ui'
#
# Created: Mon Aug 7 09:31:01 2017
# by: PyQt4 UI code generator 4.9.6
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(678, 420)
self.btnAddName = QtGui.QPushButton(Dialog)
self.btnAddName.setGeometry(QtCore.QRect(490, 310, 93, 51))
self.btnAddName.setObjectName(_fromUtf8("btnAddName"))
self.btnDelete = QtGui.QPushButton(Dialog)
self.btnDelete.setGeometry(QtCore.QRect(290, 90, 93, 28))
self.btnDelete.setObjectName(_fromUtf8("btnDelete"))
self.btnEdit = QtGui.QPushButton(Dialog)
self.btnEdit.setGeometry(QtCore.QRect(290, 140, 93, 28))
self.btnEdit.setObjectName(_fromUtf8("btnEdit"))
self.gpbGirls = QtGui.QGroupBox(Dialog)
self.gpbGirls.setGeometry(QtCore.QRect(90, 50, 171, 221))
self.gpbGirls.setObjectName(_fromUtf8("gpbGirls"))
self.lstGirls = QtGui.QListWidget(self.gpbGirls)
self.lstGirls.setGeometry(QtCore.QRect(10, 20, 151, 191))
self.lstGirls.setObjectName(_fromUtf8("lstGirls"))
self.gpbBoys = QtGui.QGroupBox(Dialog)
self.gpbBoys.setGeometry(QtCore.QRect(410, 50, 171, 221))
self.gpbBoys.setObjectName(_fromUtf8("gpbBoys"))
self.lstBoys = QtGui.QListWidget(self.gpbBoys)
self.lstBoys.setGeometry(QtCore.QRect(10, 20, 151, 192))
self.lstBoys.setObjectName(_fromUtf8("lstBoys"))
self.gpbGender = QtGui.QGroupBox(Dialog)
self.gpbGender.setGeometry(QtCore.QRect(260, 299, 211, 61))
self.gpbGender.setObjectName(_fromUtf8("gpbGender"))
self.rbMale = QtGui.QRadioButton(self.gpbGender)
self.rbMale.setGeometry(QtCore.QRect(20, 20, 71, 20))
self.rbMale.setObjectName(_fromUtf8("rbMale"))
self.rbFemale = QtGui.QRadioButton(self.gpbGender)
self.rbFemale.setGeometry(QtCore.QRect(110, 20, 81, 20))
self.rbFemale.setObjectName(_fromUtf8("rbFemale"))
self.gpbName = QtGui.QGroupBox(Dialog)
self.gpbName.setGeometry(QtCore.QRect(90, 300, 171, 61))
self.gpbName.setObjectName(_fromUtf8("gpbName"))
self.edtName = QtGui.QLineEdit(self.gpbName)
self.edtName.setGeometry(QtCore.QRect(2, 20, 161, 41))
self.edtName.setObjectName(_fromUtf8("edtName"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.btnAddName.setText(_translate("Dialog", "Add Name", None))
self.btnDelete.setText(_translate("Dialog", "Delete", None))
self.btnEdit.setText(_translate("Dialog", "Edit", None))
self.gpbGirls.setTitle(_translate("Dialog", "Girl\'s Names", None))
self.gpbBoys.setTitle(_translate("Dialog", "Boy\'s Names", None))
self.gpbGender.setTitle(_translate("Dialog", "Gender", None))
self.rbMale.setText(_translate("Dialog", "Male", None))
self.rbFemale.setText(_translate("Dialog", "Female", None))
self.gpbName.setTitle(_translate("Dialog", "Enter name:", None))

Python - Pyqt: Duplicate output when I use setCurrentIndex

When I press the button for go to a diferent page using setCurrentIndex I have problems with duplicate output values because if I set value "1" on QLineEdit on home_page for example and then go to conf_page and now I back to home_page, when I press the OK button for print content of QLineEdit for example, I get a duplicate output.
My code.
Project.py file.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(210, 191)
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(0, 0, 211, 61))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.pushButton_2 = QtGui.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(20, 20, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.frame)
self.pushButton_3.setGeometry(QtCore.QRect(110, 20, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.stackedWidget = QtGui.QStackedWidget(Dialog)
self.stackedWidget.setGeometry(QtCore.QRect(0, 60, 211, 131))
self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
self.page = QtGui.QWidget()
self.page.setObjectName(_fromUtf8("page"))
self.lineEdit = QtGui.QLineEdit(self.page)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.stackedWidget.addWidget(self.page)
self.page_2 = QtGui.QWidget()
self.page_2.setObjectName(_fromUtf8("page_2"))
self.stackedWidget.addWidget(self.page_2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton_2.setText(_translate("Dialog", "PushButton", None))
self.pushButton_3.setText(_translate("Dialog", "PushButton", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
Project.pyw file.
from project import *
import sys
class project(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL('clicked()'), self.home_page)
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL('clicked()'), self.down_page)
def home_page(self):
self.ui.stackedWidget.setCurrentIndex(0)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.output)
def down_page(self):
self.ui.stackedWidget.setCurrentIndex(1)
def output(self):
print(self.ui.lineEdit.text())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = project()
myapp.show()
sys.exit(app.exec_())
Any sugestions?
Thanks!!

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