【发布时间】:2017-08-03 10:03:50
【问题描述】:
我有两个分开运行的脚本。一个是 PyQt5 GUI 应用程序的代码,第二个是与this one 非常相似的代码,稍作修改以便能够转换内容,以防出现任何引起问题的笑脸。
基本上,当我在应用程序窗口中按下某个按钮时,我希望第二个代码会运行。
无论我如何努力适应第二个代码,它总是会导致我的应用程序(或 Python)崩溃。我能达到的最远距离是,当我关闭主窗口后第二个代码工作时 - 然后它运行,并给我想要的结果。
我怀疑这与来自第二个代码的__init__ 不高兴主窗口中已经有另一个__init__ 正在运行有关?
正如你所看到的,我对 Python 的面向对象部分非常困惑,尽管过去几天我在这个主题上努力自学,但我无法将这两个代码组合在一起。
我的应用:
#'all the necessary imports'
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.text = QWebEngineView(self)
self.proc_btn = QPushButton('Proceed')
self.userUrl = QLineEdit(self)
self.labOne = QLabel(self)
self.labTwo = QLabel(self)
self.defUrl = 'default'
self.init_ui()
def init_ui(self):
v_layout = QVBoxLayout()
h_layout = QHBoxLayout()
h_layout.addWidget(self.proc_btn)
h_layout.addWidget(self.userUrl)
v_layout.addWidget(self.text)
v_layout.addWidget(self.labOne)
v_layout.addWidget(self.labTwo)
v_layout.addLayout(h_layout)
self.labOne.setText('URL: ')
self.labTwo.setText('<ENTER LINK PLEASE>')
self.userUrl.returnPressed.connect(self.linkPut)
self.proc_btn.clicked.connect(self.doStuff)
self.setLayout(v_layout)
self.setWindowTitle('Scrapper')
self.show()
def doStuff(self):
print('Doing stuff (expecting 2nd script to be ran)')
def linkPut(self):
newText = (self.userUrl.text())
print('newText: ' + newText)
self.labTwo.setText(newText)
self.defUrl = newText
app = QApplication(sys.argv)
a_window = MainWindow()
sys.exit(app.exec_())
我需要实现的脚本:
#'all necessary imports'
class Page(QWebEnginePage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html = ''
self.loadFinished.connect(self._on_load_finished)
self.load(QUrl(url))
self.app.exec_()
print('__init__ WORKS')
def _on_load_finished(self):
self.html = self.toHtml(self.Callable)
print('Load finished')
def Callable(self, html_str):
self.html = html_str
self.app.quit()
_nonbmp = re.compile(r'[\U00010000-\U0010FFFF]')
def _surrogatepair(match):
char = match.group()
assert ord(char) > 0xffff
encoded = char.encode('utf-16-le')
return (
chr(int.from_bytes(encoded[:2], 'little')) +
chr(int.from_bytes(encoded[2:], 'little')))
def with_surrogates(text):
return _nonbmp.sub(_surrogatepair, text)
def main():
page = Page('https://somenicepage.com/')
soup = bs.BeautifulSoup(page.html, 'html.parser'))
longStrCoded = str(soup.find("img", {"class":"pictures"}))
longStr = with_surrogates(longStrCoded)
print('long str: ' + longStr)
extract = longStr.split('src="')[1].split('"')[0]
print(extract)
if __name__ == '__main__': main()
【问题讨论】:
-
import other_py_file在文件的开头,然后other_py_file.main()要触发它 -
这个我已经试过了,如果我在我的应用程序中使用它会崩溃,但是如果我先关闭我的应用程序,然后运行
other_py_file.main()它可以工作。 -
你能比“它崩溃”更具体吗
-
''Python 已停止工作 - 程序停止正常工作导致的问题。如果有可用的解决方案,Windows 将关闭程序并通知您。'' 如果我有时在语法上打错字等,也会发生这种情况。由于某种原因在使用 PyQt5 时发生。
-
那我会尽量简化这个,一步一步增加复杂度,看看是哪一步导致了问题
标签: python python-3.x class pyqt5