I am new to python and GUI programming as well.
I need to build a front-end GUI application and for the back - end application I have already built in python 2.7.
My question is, if I develop a front end application in PyQT, will I be able to integrate the python code in that (or) will the PYQT support python modules like socket, threading etc.?
PyQt doesn't handle the back-end code. It is used to set up hooks so that when a user interacts with the GUI, some code is launched in Python.
In other words, yes, you'll be able to implement things like threading.
Here's an example where I'm clicking a button which has a 'hook' to launch a process. In this case, I'm launching the process in another thread so that the GUI doesn't freeze up.
import sys
import time
import threading
import datetime as dt
# Import from qtpy if you're running Anaconda3
from qtpy import QtCore, QtGui, QtWidgets
# Import from PyQt5 otherwise
# from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(500, 220)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(400, 180, 93, 28))
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 471, 141))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtCore.QCoreApplication.translate("MainWindow", "MainWindow", None))
self.pushButton.setText(QtCore.QCoreApplication.translate("MainWindow", "Start", None))
self.label.setText(QtCore.QCoreApplication.translate("MainWindow", "", None))
def main():
# Continually update LineEdit with the time when the button is pressed
# We can do this without freezing the GUI by launching update_time()
# in another thread
ui.pushButton.clicked.connect(lambda: threading.Thread(target=update_time).start())
def update_time():
while True:
current_time_text = dt.datetime.now().strftime('%b %d %Y %I:%M:%S %p')
ui.label.setText(current_time_text)
time.sleep(.01)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
main()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.