我有兩個腳本在分離時根據需要運行。一個是PyQt5 GUI應用程序的代碼,第二個是與this one非常相似的代碼,稍作修改即可轉換內容,以防有任何笑臉導致問題。Python 3.6.2 - 多個__init__;多個類同時運行
基本上,當我按下我的應用程序窗口中的某個按鈕時,我期望第二個代碼被運行。
不管我多麼努力地嘗試適應第二個代碼,它總會使我的應用程序(或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()
'進口other_py_file'在文件的開始,然後'other_py_file.main()'要觸發它 –
這個我已經試過了,崩潰,如果我我的應用程序中使用它,但如果我關閉應用程序,然後運行'other_py_file.main()'它工作。 – NumeroSMG
你能比'它墜毀'更具體嗎 –