我一直在尝试使用Qt Designer和pyuic4生成的GUi.py文件在python中编写我的第一个应用程序。在对着这个特定的墙壁打了几个星期之后,在追求pyQt4和QT帮助文件和文档时,我不再接近解决方案。我想我可能已经在我现在肿胀和瘀伤的头上。如果有人会非常友好地给我一些基础知识,就如何将键盘输入和python app输出连接到QplainTextEdit和QTextBrowser而言,我将永远为你负债。
我已经阅读了几天关于Qt主题的内容并阅读了一两个教程,但没有给出信息。我一直在寻找。但按钮上的音量(单击),拨号,以及写入和读取XML文件。
我相信一个更有能力的人可以从我试图摄取的材料中确定,足以让他现在想出来,但显然我并不像我想的那样聪明。
提前感谢您提供的任何帮助。 Linux Ubuntu 11.10上的Python2.7,3.2 PyQt4,Pyside,PyQt-x11-gpl-4.9.1。 到目前为止,我一直在使用2.7,PyQt4。
答案 0 :(得分:0)
这是一个将现有python脚本与GUI集成的简单脚本。
import sys
from PyQt4 import QtGui, QtCore
import time
# some python function to integrate with GUI
def factorial(n):
if n in (0,1):
return 1
else:
return n*factorial(n-1)
# worker thread that will run our function
class Worker(QtCore.QThread):
# custom signal that will be emitted when an output is ready
# 'int' instead of 'object' will result in C++ int and might overflow.
# object makes sure it is a python object
resultReady = QtCore.pyqtSignal(object)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
def setUp(self, n):
self.n = n
def run(self):
time.sleep(1) # dummy sleep to emulate 'working' status
self.result = factorial(self.n)
self.resultReady.emit(self.result)
# Main GUI window
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
# setup widgets
self.input = QtGui.QLineEdit()
self.output = QtGui.QLineEdit()
self.button = QtGui.QPushButton('Calculate Factorial')
self.button.clicked.connect(self.calculate)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.input)
layout.addWidget(self.output)
layout.addWidget(self.button)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# setup worker thread and connect signal
self.worker = Worker(self)
self.worker.resultReady.connect(self.updateOutput)
def calculate(self):
try:
n = int(self.input.text())
# setup thread
self.worker.setUp(n)
# start thread
self.worker.start()
except ValueError:
pass
def updateOutput(self, result):
# will be called when we have a result
# and put that result in appropriate widget
self.output.setText('%d' % result)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())