The value obtained from lineEdit cannot be passed to the thread

77 views Asked by At

I try to pass the value obtained from lineEdit to the thread DBLP_thread, but the thread cannot receive this value. However, if I manually define a string that can be passed to the thread, what is going on? How can I pass the value obtained from lineEdit to the thread?Below is my code.

from PySide2.QtWidgets import QApplication, QMessageBox, QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2 import QtWidgets
from PySide2.QtCore import QThread, Signal, QThreadPool

import sys
import re
import threading
import requests
import time

class My_Window():
  def __init__(self):
    self.ui = QUiLoader().load('demo.ui')
    self.ui.pushButton.clicked.connect(self.box)

  def DBLP_thread_callback(self, title, web):
    # self.ui.pushButton.setText(i)
    for i in range(len(title)):
        print(title[i])
        print(web[i])
    self.ui.pushButton.setChecked(False)
    self.ui.pushButton.setDisabled(False)

  def box(self):
    self.keys = self.ui.lineEdit.text() #get value from lineEdit
    print(self.keys)
    self.ui.pushButton.setChecked(True)
    self.ui.pushButton.setDisabled(True)
    self.th = DBLP_thread(ip='192.168.1.1', port=40)  #start my thread
    self.th.signal.connect(self.DBLP_thread_callback)
    self.th.start()

class DBLP_thread(QThread):
  signal = Signal(list, list)

  def __init__(self, ip, port, parent=None):
    super(DBLP_thread, self).__init__(parent)
    self.ip = ip
    self.port = port

  def run(self):
    '''
    重写
    '''
    time.sleep(1)
    self.win = My_Window()
    self.win.box()
    print(self.win.keys)  #Get the keys in the box(), but I did not get the value
    page = requests.get('https://dblp.uni-trier.de/search?q=' + self.win.keys)
    self.paper_title = re.findall('<span class="title" itemprop="name">(.*?)</span>', page.text, 
         re.S)
    self.paper_web = re.findall('<nav class="publ".*?li class="drop-down".*?div class="head".*?href=" 
          (.*?)">',page.text,re.S)
    self.signal.emit(self.paper_title, self.paper_web)
    return
0

There are 0 answers