我也是python和GUI编程的新手。
我需要构建一个前端GUI应用程序,并且我已经在python 2.7中构建了后端应用程序。
我的问题是,如果我在PyQT中开发前端应用程序,我是否能够在那个(或)中集成python代码,PYQT是否支持套接字,线程等python模块?
答案 0 :(得分:0)
PyQt不处理后端代码。它用于设置钩子,以便当用户与GUI交互时,一些代码在Python中启动。
换句话说,是的,您将能够实现线程之类的功能。
以下是我点击按钮的示例,其中有一个' hook'启动一个过程。在这种情况下,我在另一个线程中启动该过程,以便GUI不会冻结。
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_())