Pyqt4 age calculator does not output on label - python

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

Related

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

PyQt transferring a list between dialog and window

Very much a beginner on the UI side of things!
I'm trying to transfer a list from a dialog to the main window (when a button is pressed) and then transfer a new list back to the window when the dialog is accepted.
Here's a MWE of what I'm working on to give you an idea.
It should print out the following,
Desired output:
[b]
.
[b]
[a]
.
[a]
Achieved output:
[b]
.
[b]
[a]
.
[b]
Thanks in advance!
MWE:
import sys
from PyQt4 import QtCore, QtGui#pyqt5, QtWidgets
from Scrabble_gui import Ui_MainWindow
from Input_letters_gui import Ui_Dialog as Popup
class ScrabbleGuiProgram(Ui_MainWindow):
def __init__(self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
self.In_letters.clicked.connect(self.Input_letters)
def Input_letters(self):
"""Prints "b" and opens pop up.
after popup is closed, prints "a"
"""
letter_list = ["b"]
#app = QtGui.QApplication(sys.argv)
print(letter_list)
print(".")
PUDialog = QtGui.QDialog()
pop = Input_letters_popup(PUDialog, letter_list)
PUDialog.exec_()
print(".")
print(letter_list)
class Input_letters_popup(Popup):
def __init__(self, dialog, letter_list):
"""when open prints incoming list ("b")
"""
print(letter_list)
Popup.__init__(self)
self.setupUi(dialog)
self.buttonBox.accepted.connect(self.Get_letters)#lambda: self.Get_letters)
#self.buttonBox.rejected.connect()
def Get_letters(self):
"""on popup exit prints new list ("a")
"""
letters = ["a"]
print(letters)
return letters
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QMainWindow()
prog = ScrabbleGuiProgram(Dialog)#Input_letters_popup(Dialog)
Dialog.show()
sys.exit(app.exec_())
Scrabble_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Scrabble.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(406, 267)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Gen_words = QtGui.QPushButton(self.centralwidget)
self.Gen_words.setGeometry(QtCore.QRect(250, 20, 121, 28))
self.Gen_words.setObjectName(_fromUtf8("Gen_words"))
self.Long_word = QtGui.QLineEdit(self.centralwidget)
self.Long_word.setGeometry(QtCore.QRect(229, 100, 141, 22))
self.Long_word.setReadOnly(True)
self.Long_word.setObjectName(_fromUtf8("Long_word"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(279, 180, 41, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(99, 143, 131, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.High_score = QtGui.QLineEdit(self.centralwidget)
self.High_score.setGeometry(QtCore.QRect(319, 180, 51, 22))
self.High_score.setReadOnly(True)
self.High_score.setObjectName(_fromUtf8("High_score"))
self.High_word = QtGui.QLineEdit(self.centralwidget)
self.High_word.setGeometry(QtCore.QRect(229, 143, 141, 22))
self.High_word.setReadOnly(True)
self.High_word.setObjectName(_fromUtf8("High_word"))
self.In_letters = QtGui.QPushButton(self.centralwidget)
self.In_letters.setGeometry(QtCore.QRect(90, 57, 121, 28))
self.In_letters.setObjectName(_fromUtf8("In_letters"))
self.Gen_letters = QtGui.QPushButton(self.centralwidget)
self.Gen_letters.setGeometry(QtCore.QRect(90, 20, 121, 28))
self.Gen_letters.setObjectName(_fromUtf8("Gen_letters"))
self.letters = QtGui.QListWidget(self.centralwidget)
self.letters.setGeometry(QtCore.QRect(20, 50, 41, 161))
self.letters.setObjectName(_fromUtf8("letters"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(145, 100, 81, 20))
self.label.setObjectName(_fromUtf8("label"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 20, 41, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Descrabbler", None))
self.Gen_words.setText(_translate("MainWindow", "Best Words", None))
self.label_2.setText(_translate("MainWindow", "Score", None))
self.label_3.setText(_translate("MainWindow", "Highest Scoring Word", None))
self.In_letters.setText(_translate("MainWindow", "Input Letters", None))
self.Gen_letters.setText(_translate("MainWindow", "Generate Letters", None))
self.label.setText(_translate("MainWindow", "Longest Word", None))
self.label_4.setText(_translate("MainWindow", "Letters", 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_())
Input_letters_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Input_letters.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(400, 173)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 90, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.ltr1 = QtGui.QLineEdit(Dialog)
self.ltr1.setGeometry(QtCore.QRect(20, 40, 31, 22))
self.ltr1.setObjectName(_fromUtf8("ltr1"))
self.ltr2 = QtGui.QLineEdit(Dialog)
self.ltr2.setGeometry(QtCore.QRect(60, 40, 31, 22))
self.ltr2.setObjectName(_fromUtf8("ltr2"))
self.ltr3 = QtGui.QLineEdit(Dialog)
self.ltr3.setGeometry(QtCore.QRect(100, 40, 31, 22))
self.ltr3.setObjectName(_fromUtf8("ltr3"))
self.ltr4 = QtGui.QLineEdit(Dialog)
self.ltr4.setGeometry(QtCore.QRect(140, 40, 31, 22))
self.ltr4.setObjectName(_fromUtf8("ltr4"))
self.ltr5 = QtGui.QLineEdit(Dialog)
self.ltr5.setGeometry(QtCore.QRect(180, 40, 31, 22))
self.ltr5.setObjectName(_fromUtf8("ltr5"))
self.ltr6 = QtGui.QLineEdit(Dialog)
self.ltr6.setGeometry(QtCore.QRect(220, 40, 31, 22))
self.ltr6.setObjectName(_fromUtf8("ltr6"))
self.ltr7 = QtGui.QLineEdit(Dialog)
self.ltr7.setGeometry(QtCore.QRect(260, 40, 31, 22))
self.ltr7.setObjectName(_fromUtf8("ltr7"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Input Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

Qt GUI clickable Qframe or QWidget or Groupbox

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

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

pyqt 'Ui_Form' object has no attribute 'show'

this is my fill
this code is generated by pyuic
im using pyqt4
and python 2.7
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'editgui.ui'
#
# Created: Mon Nov 24 17:33:07 2014
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import PyQt4
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(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.setEnabled(True)
Form.resize(1032, 779)
Form.setMinimumSize(QtCore.QSize(1032, 779))
Form.setMaximumSize(QtCore.QSize(1032, 779))
self.textEdit = QtGui.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(90, 110, 361, 221))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.textEdit_2 = QtGui.QTextEdit(Form)
self.textEdit_2.setGeometry(QtCore.QRect(90, 430, 361, 261))
self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(90, 380, 361, 20))
...
self.label_6.setGeometry(QtCore.QRect(510, 90, 46, 13))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.pushButton.show)
QtCore.QObject.connect(self.textEdit, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.pushButton.show)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.pushButton.show)
QtCore.QObject.connect(self.textEdit_2, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.pushButton.show)
QtCore.QObject.connect(self.lineEdit_3, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.pushButton.show)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "titel", None))
self.label_2.setText(_translate("Form", "beschrijving", None))
self.label_3.setText(_translate("Form", "frans", None))
self.label_4.setText(_translate("Form", "titel", None))
self.label_5.setText(_translate("Form", "beschrijving", None))
self.pushButton.setText(_translate("Form", "save", None))
self.pushButton_2.setText(_translate("Form", "run", None))
self.label_6.setText(_translate("Form", "website", None))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_Form()
ex.show
sys.exit(app.exec_())
end this is the error i get
Traceback (most recent call last):
File "C:\Users\IT4PROGRESS\Desktop\2dehands gui\output.py", line 99, in <module>
ex.show
AttributeError: 'Ui_Form' object has no attribute 'show'
i use python 2.7
Firstly, don't edit file generated by pyuic. Make another .py file and import it. That way, instead of trying to show() the generated UI file directly, you can make a QMainWindow based class that you can run show() on and it will build the generated ui file for you. Like this:
import sys
from PyQt4 import QtCore, QtGui
from Ui_Form import Ui_Form
class Main(QtGui.QMainWindow):
def __init__(self):
super(Main, self).__init__()
# build ui
self.ui = Ui_Form()
self.ui.setupUi(self)
# connect signals
self.ui.some_button.connect(self.on_button)
def on_button(self):
print 'Button clicked!'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

Categories