PyQt Web 浏览器:无需单击鼠标即可专注于网页

发布于 2024-11-17 12:12:52 字数 621 浏览 6 评论 0原文

我正在为没有鼠标输入的嵌入式设备编写一个应用程序。 Web 浏览器非常非常基本,没有任何按钮、地址栏、文件栏等。目前,它只是加载一个网页。该网页使用 JavaScript 来捕获按键事件以进行操作。

我遇到的问题是,当浏览器加载时,按键未被捕获。我已经将这个问题归结为我认为的焦点问题。加载浏览器时,只有在应用程序上单击鼠标才能获得焦点。由于我没有鼠标,因此无法发生初始单击。

如何确保浏览器应用程序正确聚焦,以便当我从终端或脚本启动它时,它将立即获得焦点并且关键事件可以相应发生?

我的代码如下:

#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())

假设我正确使用它,QWidget.setFocus() 不起作用。任何帮助表示赞赏。谢谢

I am writing an application for an embedded device that has no mouse input. The web browser is very very basic and does not have any buttons, address bar, file bar , etc. For now, it simply just loads a web page. This web page uses javascript to catch key press events for actions.

The problem I am having is that when the browser loads, the key presses are not caught. I have tracked this problem down to what I believe is a focus problem. When the browser loads, it does not have focus until a mouse click occurs on the application. Since I do not have a mouse, that initial click cannot happen.

How can I make sure that the browser application gets focused correctly such that when I launch it from a terminal or script it will gain immediate focus and the key events can happen accordingly?

My code is as follows:

#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())

The QWidget.setFocus() did not work, assuming I used it correctly. Any help is appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-11-24 12:12:53

我在我的笔记本电脑上尝试了你的代码,QWebView 已经获得焦点 - 一旦 Google 加载,我就可以输入并且我的文本出现在文本框中。

如果是焦点问题,那么由于 QWebView 继承了 QWidget,您可以调用其

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.loadFinished.connect(web.setFocus)
web.showFullScreen()
web.load(QUrl("http://www.google.com/"))
sys.exit(app.exec_())

I tried your code on my laptop and the QWebView had focus already - once Google had loaded I could type and my text appeared in the text box.

If it is a focus problem then as QWebView inherits QWidget you can call its setFocus() method. Here is your code with an extra line calling the QWebView's setFocus method once a page has loaded:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()
web.loadFinished.connect(web.setFocus)
web.showFullScreen()
web.load(QUrl("http://www.google.com/"))
sys.exit(app.exec_())
甜宝宝 2024-11-24 12:12:53

您可以将焦点设置在主框架上:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *


if __name__ == '__main__':
    app = QApplication(sys.argv)
    web = QWebView()
    frame = web.page().mainFrame()
    frame.setFocus()
    web.showFullScreen()
    web.load(QUrl("http://www.google.com"))
    sys.exit(app.exec_())

You can set focus on the main frame:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *


if __name__ == '__main__':
    app = QApplication(sys.argv)
    web = QWebView()
    frame = web.page().mainFrame()
    frame.setFocus()
    web.showFullScreen()
    web.load(QUrl("http://www.google.com"))
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文