- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 Python 显示一个 2D map ,然后在 Python 代码中用 coursor 的坐标做一些事情。但是,我无法获得 Python 部分的坐标。这是我的代码:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QWidget,QVBoxLayout, QApplication
from PyQt5.QtWebChannel import QWebChannel
import bs4
maphtml = '''
<!DOCTYPE HTML>
<!DOCTYPE HTML>
<html>
<head>
<meta name="robots" content="index, all" />
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
<title>WebGL Earth API - Side-by-side - Basic Leaflet compatibility</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="http://www.webglearth.com/v2/api.js"></script>
<script>
var backend;
new QWebChannel(qt.webChannelTransport, function (channel) {
backend = channel.objects.backend;
});
function init() {
var m = {};
start_(L, 'L');
start_(WE, 'WE');
function start_(API, suffix) {
var mapDiv = 'map' + suffix;
var map = API.map(mapDiv, {
center: [51.505, -0.09],
zoom: 4,
dragging: true,
scrollWheelZoom: true,
proxyHost: 'http://srtm.webglearth.com/cgi-bin/corsproxy.fcgi?url='
});
m[suffix] = map;
//Add baselayer
API.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
attribution: '© OpenStreetMap contributors'
}).addTo(map);
//Add TileJSON overlay
var json = {"profile": "mercator", "name": "Grand Canyon USGS", "format": "png", "bounds": [-112.26379395, 35.98245136, -112.10998535, 36.13343831], "minzoom": 10, "version": "1.0.0", "maxzoom": 16, "center": [-112.18688965, 36.057944835, 13], "type": "overlay", "description": "", "basename": "grandcanyon", "tilejson": "2.0.0", "sheme": "xyz", "tiles": ["http://tileserver.maptiler.com/grandcanyon/{z}/{x}/{y}.png"]};
if (API.tileLayerJSON) {
var overlay2 = API.tileLayerJSON(json, map);
} else {
//If not able to display the overlay, at least move to the same location
map.setView([json.center[1], json.center[0]], json.center[2]);
}
//Add simple marker
var marker = API.marker([json.center[1], json.center[0]]).addTo(map);
marker.bindPopup(suffix, 50);
marker.openPopup();
//Print coordinates of the mouse
map.on('mousemove', function(e) {
document.getElementById('coords').innerHTML = e.latlng.lat + ', ' + e.latlng.lng;
backend.print(e.latlng.lat)
});
}
//Synchronize view
m['L'].on('move', function(e) {
var center = m['L'].getCenter();
var zoom = m['L'].getZoom();
m['WE'].setView([center['lat'], center['lng']], zoom);
});
}
</script>
<style>
html, body{padding: 0; margin: 0; overflow: hidden;}
#mapL, #mapWE {position:absolute !important; top: 0; right: 0; bottom: 0; left: 0;
background-color: #fff; position: absolute !important;}
#mapL {right: 0%;}
#mapWE {left: 100%;}
#coords {position: absolute; bottom: 0;}
</style>
</head>
<body onload="javascript:init()">
<div id="mapL"></div>
<div id="mapWE"></div>
<div id="coords"></div>
</body>
</html>
'''
class Browser(QApplication):
def __init__(self):
QApplication.__init__(self, [])
self.window = QWidget()
self.window.setWindowTitle("Serial GPS Emulator");
self.web = QWebEngineView(self.window)
self.web.setHtml(maphtml)
self.layout = QVBoxLayout(self.window)
self.layout.addWidget(self.web)
self.window.show()
self.exec()
Browser()
如果代码保留在一个文件中会很好,但如果完全不可能在一次拆分中完成,那也是可以接受的。我想解决 mny 问题的第一步是从 HTML/JS 部分调用一个函数,因为 backend.print("test") 也不起作用。我还注意到 self.exec() 阻止了其余代码,有没有办法在 map 运行时执行任何其他代码?谢谢!
最佳答案
我没有看到您在代码中的哪个位置传递了后端。
在这种情况下,您必须创建一个可以注入(inject)的 Backend
类,并且对于要调用的方法,它必须是一个插槽,为此您必须使用 pyqtSlot()
设置,它接收的参数取决于您发送的内容,在 e.latlng
的情况下是 QJsonValue
。在插槽中,您必须分离必要的部分。
import sys
from PyQt5.QtCore import pyqtSlot, QObject, QJsonValue, pyqtSignal, QTimer
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
maphtml = '''
<!DOCTYPE HTML>
<!DOCTYPE HTML>
<html>
<head>
<meta name="robots" content="index, all" />
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
<title>WebGL Earth API - Side-by-side - Basic Leaflet compatibility</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="http://www.webglearth.com/v2/api.js"></script>
<script>
var backend;
new QWebChannel(qt.webChannelTransport, function (channel) {
backend = channel.objects.backend;
console.log(backend);
});
function init() {
var m = {};
start_(L, 'L');
start_(WE, 'WE');
function start_(API, suffix) {
var mapDiv = 'map' + suffix;
var map = API.map(mapDiv, {
center: [51.505, -0.09],
zoom: 4,
dragging: true,
scrollWheelZoom: true,
proxyHost: 'http://srtm.webglearth.com/cgi-bin/corsproxy.fcgi?url='
});
m[suffix] = map;
//Add baselayer
API.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
attribution: '© OpenStreetMap contributors'
}).addTo(map);
//Add TileJSON overlay
var json = {"profile": "mercator", "name": "Grand Canyon USGS", "format": "png", "bounds": [-112.26379395, 35.98245136, -112.10998535, 36.13343831], "minzoom": 10, "version": "1.0.0", "maxzoom": 16, "center": [-112.18688965, 36.057944835, 13], "type": "overlay", "description": "", "basename": "grandcanyon", "tilejson": "2.0.0", "sheme": "xyz", "tiles": ["http://tileserver.maptiler.com/grandcanyon/{z}/{x}/{y}.png"]};
if (API.tileLayerJSON) {
var overlay2 = API.tileLayerJSON(json, map);
} else {
//If not able to display the overlay, at least move to the same location
map.setView([json.center[1], json.center[0]], json.center[2]);
}
//Add simple marker
var marker = API.marker([json.center[1], json.center[0]]).addTo(map);
marker.bindPopup(suffix, 50);
marker.openPopup();
//Print coordinates of the mouse
map.on('mousemove', function(e) {
document.getElementById('coords').innerHTML = e.latlng.lat + ', ' + e.latlng.lng;
backend.print(e.latlng)
});
}
//Synchronize view
m['L'].on('move', function(e) {
var center = m['L'].getCenter();
var zoom = m['L'].getZoom();
m['WE'].setView([center['lat'], center['lng']], zoom);
});
}
</script>
<style>
html, body{padding: 0; margin: 0; overflow: hidden;}
#mapL, #mapWE {position:absolute !important; top: 0; right: 0; bottom: 0; left: 0;
background-color: #fff; position: absolute !important;}
#mapL {right: 0%;}
#mapWE {left: 100%;}
#coords {position: absolute; bottom: 0;}
</style>
</head>
<body onload="javascript:init()">
<div id="mapL"></div>
<div id="mapWE"></div>
<div id="coords"></div>
</body>
</html>
'''
class Backend(QObject):
positionChanged = pyqtSignal(float, float)
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.position = None, None
self.timer = QTimer(self)
self.timer.setInterval(1000)
self.timer.timeout.connect(self.on_timeout)
@pyqtSlot(QJsonValue)
def print(self, val):
coords = val.toObject()
lat, lng = (coords[key].toDouble() for key in ("lat", "lng"))
self.position = lat, lng
if not self.timer.isActive():
self.timer.start()
def on_timeout(self):
self.positionChanged.emit(*self.position)
def foo(lat, lng):
# this function will be called every second.
print(lat, lng)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QWebEngineView()
view.setWindowTitle("Serial GPS Emulator");
backend = Backend(view)
backend.positionChanged.connect(foo)
channel = QWebChannel()
channel.registerObject('backend', backend)
view.page().setWebChannel(channel)
view.setHtml(maphtml)
view.show()
sys.exit(app.exec_())
关于javascript - 如何将值从 JS 传回 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49760249/
昨天开始学习ansible,所以我相信我可能会在这里冒XY问题的风险,但仍然...... 主要的yml: - hosts: localhost vars_files: [ "users.y
我在 Storyboard中创建了一些segue。它有一些标识符。我在方法 prepareForSegue:sender: 中配置 destinationViewController 并分配一些对象作
我想用 Promises 替换代码中的一些回调。 我有一个使用 setAnimationFrame 在循环中运行的模型。我想在我的模型中启动一些东西,并在完成时通知我。目前我是这样做的。 doSome
我需要动态获取用户选择的任何选项的值,并在单击按钮时将该值传递回 Facebook Pixel。 请参阅脚本区域中的所选选项值。 Wh
这个问题在这里已经有了答案: dismissModalViewController AND pass data back (4 个回答) 9年前关闭。 我有一个带有表格的popoverview,我想在
我想用 Python 显示一个 2D map ,然后在 Python 代码中用 coursor 的坐标做一些事情。但是,我无法获得 Python 部分的坐标。这是我的代码: from PyQt5.Qt
我正在使用 NXP lpc1769 演示板开发一个小项目。我正在使用 CodeRed 的 LPCExpresso 5 对其进行编程,我想知道是否可以将应用程序 hex/bin 文件从开发板传输回 PC
我是 Android 的新手,一般来说是事件驱动代码。我没有在 Activity 中嵌入大量匿名事件监听器类来处理 onClick 事件等,而是定义了单独的类以保持代码整洁。然后我使用它们,例如像这样
我已经成功地使用了 prepareForSegue,不过我已经成功地传递了变量。我现在正在尝试通过 segue 的反向传递 NSNumber 但 prepareForSegue 没有被调用。要返回到我
我有 ViewController 和 TableViewController。在 ViewController 中,我有 2 个按钮和 2 个文本字段,当单击按钮 1 时,它将导航到带有静态数据的
我如何将信息从 Jinja 模板页面传回 Flask? 假设我打印了一些项目列表。用户选择项目,我可以通过 Javascript 捕捉。 将所选项目作为参数传递给将生成该项目自己的页面的函数的最佳做法
我怎样才能将数据从 php 的然后行传回 ajax? PHP $query = 'SELECT * FROM picture order by rand() LIMIT 10'; $result =
在我的 MVC4 项目 View 中,正在传递 IEnumerable模型来查看。当我将表单提交回 Controller 时, Controller 方法的参数为 IEnumerable data 。
在我的 MVC4 项目 View 中,正在传递 IEnumerable要查看的模型。当我将表单提交回我的 Controller 时, Controller 方法的参数为 IEnumerable dat
我试图将订单 ID 从订单页面传递到 Paypal,然后返回到感谢页面,这样我就可以将订单标记为已付款,但我遇到了问题。目前我将这些变量传递给 Paypal,如下所示: $vars = array(
所以通常我会为此使用委托(delegate)模式,但这是一个棘手的情况。 View Controller A 呈现 -> View Controller B 呈现 -> View Controller
我是 Android 和 Java 的初学者。到目前为止还不错,但我偶然发现了一个我无法解决的问题。 我正在尝试在我的应用程序类中创建一个方法,该方法将使用传递给它的值对列表进行 http 调用。这是
在我的应用程序中,我使用 uitable 从我的列表中选择类别。我的任务是,当用户单击或选择一个单元格时,他应该能够在下一个 View (详细 View )中查看所选单元格的详细信息。当他在详细 Vi
我正在构建一个应用程序,当用户从 select 标签中选择一个值时,系统会提示用户使用另一个带有相应数据的 select 标签。我通过从第一个字段中获取数据并使用 Ajax 将其传回我的 Contro
我有一个 C# 应用程序需要获取从 C++ DLL 传回的可变长度数据。通常,我按如下方式解决此问题:对于每个实体类型(例如字符串或位图),我都有一个适当类型的持久 C++ 变量。然后,C# 应用程序
我是一名优秀的程序员,十分优秀!