I am trying to create a phone dialer using python and Iam using QT Designer to do it.
I have laid out a simple UI and I have also exported the code of the .ui file to python code (see code here: https://pastebin.com/bPYjnU0k ).
The issue I am having is I cannot figure out how to get the "phone number" to display in the QLineEdit when the buttons are pressed on the keypad. Here is a code snippet of my code showing the button dialogs (again: full code at pastebin in the link above):
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton_numZero.setText(_translate("Dialog", "0", None))
self.pushButton_num4.setText(_translate("Dialog", "4", None))
self.pushButton_num3.setText(_translate("Dialog", "3", None))
self.pushButton_num5.setText(_translate("Dialog", "5", None))
self.pushButton_num6.setText(_translate("Dialog", "6", None))
self.pushButton_num1.setText(_translate("Dialog", "1", None))
self.pushButton_num9.setText(_translate("Dialog", "9", None))
self.pushButton_num2.setText(_translate("Dialog", "2", None))
self.pushButton_num7.setText(_translate("Dialog", "7", None))
self.pushButton_num8.setText(_translate("Dialog", "8", None))
self.pushButton_numStar.setText(_translate("Dialog", "*", None))
self.pushButton_numPound.setText(_translate("Dialog", "#", None))
self.pushButton_del.setText(_translate("Dialog", "DEL", None))
self.pushButton_call.setText(_translate("Dialog", "Call", None))
self.number_display.setPlaceholderText(_translate("Dialog", "Dial Number ...", None))
self.pushButton_Contacts.setText(_translate("Dialog", "Plp", 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_())
How do I hook the dial pad buttons up to make the "phone number" display when I press the buttons to "dial" the phone number?
Thanks.
As PyQt points out in the docs it is not recommended to modify the file generated by Qt Designer, so I will assume that this file is called ui_dialog.py. Going to the problem, you must connect the clicked signal of each button and according to the button, implement the logic of adding or deleting text, etc.
main.py
from PyQt4 import QtCore, QtGui
from ui_dialog import Ui_Dialog
class Dialog(QtGui.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
buttons = (
self.pushButton_num1, self.pushButton_num2, self.pushButton_num3,
self.pushButton_num4, self.pushButton_num5, self.pushButton_num6,
self.pushButton_num7, self.pushButton_num8, self.pushButton_num9,
self.pushButton_numStar, self.pushButton_numZero, self.pushButton_numPound,
self.pushButton_call, self.pushButton_del, self.pushButton_Contacts)
for btn in buttons:
btn.clicked.connect(self.on_clicked)
@QtCore.pyqtSlot()
def on_clicked(self):
current_text = self.number_display.text()
t = self.sender().text()
if t in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "#"):
self.number_display.setText(current_text + t)
elif t == "DEL":
self.number_display.setText(current_text[:-1])
elif t == "Call":
print("Call to: "+ current_text)
elif t == "Plp":
print("Plp")
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())