【问题标题】:Return value from PYQt slot of QwebView to main window widget (PYQt python)从 QwebView 的 PYQt 插槽返回值到主窗口小部件(PYQt python)
【发布时间】:2016-08-15 17:22:04
【问题描述】:

我正在使用具有 QwebBrowser 的 PyQt python 构建桌面应用程序。现在我正在使用 javascript 运行一些函数,该函数根据以下示例返回一个值,例如 abc。

class QWebView(QWebView):

 def contextMenuEvent(self,event):  
      menu = QMenu()
      self.actionShowXpath = menu.addAction("Show Xpath")  
      QObject.connect(self.actionShowXpath,SIGNAL("triggered()"),self,SLOT("slotshowxpath()"))

      menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))   

      @pyqtSlot()
      def slotshowxpath(self):
           frame.evaluateJavaScript("var abc = function get()");
           result = frame.evaluateJavaScript("abc").toString()

            **some code code to put result in QLineEdit Widget**
            # something like below
            # xpath.setText(result)


def window():
   app = QtGui.QApplication(sys.argv)
   w = QtGui.QWidget()
   web = QWebView(w)
   web.load(QUrl("http://www.indiatimes.com/"))
   web.show()

   xpath = QtGui.QLineEdit("", w)
   sys.exit(app.exec_())

if __name__ == '__main__':
     window()

现在,我想将 abc 的值放入我的应用程序中的 QLineEdit 小部件(“xpath”)中。请给我建议,我该怎么做?

【问题讨论】:

  • js中函数不是这样调用的。
  • @salomonderossi 感谢您的参考,但我想知道如何将结果的值放入另一个定义中存在的 QLineEdit 小部件中。??
  • @salomonderossi 如果您对这个问题有任何想法,请提供帮助
  • @7stud 请看一下。

标签: python pyqt signals-slots qwidget qwebview


【解决方案1】:

我无法举一个例子,因为 QtWebkit 已从 Qt 5.6 中删除,但如果您遇到的问题是因为您没有对 QLineEdit 的引用,那么将 QLineEdit 传递给您的 QWebView 类的 @987654321 @函数:

def start_app():
    app = QtGui.QApplication(sys.argv)

    main_window = QtGui.QWidget()
    xpathInput = QtGui.QLineEdit(main_window)

    web_view = MyWebView(main_window, xpathInput)  #<===HERE
    web_view.load(QUrl("http://www.indiatimes.com/"))

    main_window.show()
    sys.exit(app.exec_())

然后在你的 QWebView 类中:

class MyWebView(QWebView):
    def __init__(self, parent, xpath_widget):
        #QWebView.__init__(parent)
        QWebView.__init__(self, parent)
        #or: super(MyWebView, self).__init__(parent)

        self.xpath_widget = xpath_widget

    def contextMenuEvent(self,event):  
        menu = QMenu()
        self.actionShowXpath = menu.addAction("Show Xpath") 

        #QObject.connect(
        #    self.actionShowXpath,
        #    SIGNAL("triggered()"),
        #    self,SLOT("slotshowxpath()")
        #)

        self.actionShowXpath.triggered.connect(self.show_xpath)

        menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))   

    #@pyqtSlot()
    def show_xpath(self):
        frame = ...
        frame.evaluateJavaScript("var abc = function get()");
        result = frame.evaluateJavaScript("abc").toString()

        #some code code to put result in QLineEdit Widget**
        self.xpath_widget.setText(result)

但我认为组织代码的更好方法是执行以下操作:

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.xpathInput = QtGui.QLineEdit(self)

        self.web_view = QWebView(self) 
        self.web_view.load(QUrl("http://www.indiatimes.com/"))

        self.menu = QMenu()
        self.actionShowXpath = self.menu.addAction("Show Xpath") 

        #QObject.connect(
        #    self.actionShowXpath,
        #    SIGNAL("triggered()"),
        #    self,SLOT("slotshowxpath()")
        #)

        self.actionShowXpath.triggered.connect(self.show_xpath)

        menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))

    def show_path(self):
        frame = ...
        result = frame.evaluateJavaScript("abc").toString()
        self.xpathInput.setText(result)


def start_app():
    app = QtGui.QApplication(sys.argv)
    main_window = MyWindow()
    main_window.show()
    sys.exit(app.exec_())

【讨论】:

  • 感谢您的帮助,我会在几个小时内尝试并更新您
  • 它向我显示了一个错误 - Traceback(最近一次调用最后一次):文件“GUI-Test.py”,第 91 行,在 window() 文件“GUI-Test.py”中,第 75 行,在窗口中 web = QWebView(w,xpath) TypeError: __init__() 恰好需要 2 个参数(给定 3 个)
  • @ricky rana,GUI 编程是计算机编程中的中级到高级主题。很明显,您是 python 初学者。你还没有准备好使用 PyQt 程序。不过,请参阅我所做的更改。
  • 你是对的,我是 python 新手,但别担心,我很快就会成为像你一样的优秀程序员,感谢您的帮助,但很抱歉再次显示错误 - 文件“GUI -Test.py",第 12 行,在 init QWebView.__init__(parent) TypeError: unbound method __init__() must be called with QWebView instance as first argument (得到 QWidget instance).
  • @ricky 拉纳:QWebView.__init__(self, parent)。或者,您也许可以使用 super():super(MyWebView, self).__init__(parent)
猜你喜欢
  • 1970-01-01
  • 2011-08-26
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多