【发布时间】:2017-10-11 18:26:20
【问题描述】:
我正在关注 this tutorial 的 PyQt5 GUI 窗口:
这是我在 pastebin 上的代码。
import sys
from PyQt5 import QtWidgets, QtGui
def window1():
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
b = QtWidgets.QPushButton('Comparison Report', w)
l1 = QtWidgets.QLabel(w)
l2 = QtWidgets.QLabel(w)
l1.setText('Main Page')
b.setGeometry(200, 100, 300, 70)
w.setWindowTitle('Diff Util')
w.setGeometry(800, 200, 720, 800)
l1.move(310, 5)
w.show()
sys.exit(app.exec_())
#import file_report
#def on_click(self):
#file_report()
window1()
这里也是 pastebin 上的比较文件脚本...但我需要 10rep 来链接它>_>
import sys
import os
import difflib
first_file = input("Select original file: ")
second_file = input("Select second file for comparison: ")
first_file_lines = open(first_file).readlines()
second_file_lines = open(second_file).readlines()
difference = difflib.HtmlDiff(tabsize=8,
wrapcolumn=100).make_file(first_file_lines, second_file_lines, first_file, second_file, charset='utf-8')
difference_report = open('difference_report.html', 'w')
difference_report.write(difference)
difference_report.close()
os.startfile('difference_report.html')
我的问题是,如何将我的 file_report.py 连接到我用 PyQt5 创建的按钮?
如您所见,我注释掉了“import file_report”,因为据我了解,我应该导入我的脚本......但导入会导致它在终端中运行脚本,执行后将打开我的 GUI。我想运行此脚本,但将其包含在我创建的 GUI 窗口中,而不是打开终端执行。
那么我应该在 PyQt5 脚本的哪个位置导入,并包含 .py 文件?
【问题讨论】:
-
input() 是一个阻塞任务,因此不建议在 GUI 中使用它,作为替换该任务的组件的 GUI,在 PyQt 的情况下是 QLineEdit。
-
好吧,所以我必须将 input() 方法更改为 PyQt5 可以为自己的用户输入解释的方法?所以这意味着我必须重写我的脚本以包含 PyQt5 元素?
-
是的,这就是我想要的。
-
我可以帮助你,但我有一个问题,我不明白 startfile(),Linux 不存在,但如果你告诉我你是做什么的,你可以举个例子
-
@zero01alpha QtPy 是什么?
标签: python python-3.x pyqt pyqt5