绘图 PyQt Painter

发布于 2024-11-07 10:57:39 字数 1304 浏览 0 评论 0原文

我正在尝试用 pyqt 绘制地图,但它不起作用。到目前为止,我要么没有输出,要么收到诸如 Seg 错误之类的错误。

这是我现在使用的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(0, 0, 500, 500)
        self.setWindowTitle('Painel')
        list_ = []
        file_ = open('points.txt')
        for line in file_.readlines():
            l = line.replace("\n", "")
            l = l.split(" ")
            try:
                l = [float(i) for i in l]
                list_.append(l)
            except: pass#possible strings
        first = list_[0]
        list_ = list_[1:]
        self.path = QPainterPath()
        self.path.moveTo(*first)
        for i in list_:
            self.path.lineTo(*i)

    def paintEvent(self, e):      
        qp = QPainter()

        qp.begin(self)
        qp.drawPath(self.path)
        qp.end()


app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()

[编辑]这是points.txt的一些内容

-57.328 -29.972
-57.323 -29.937
-57.329 -29.895
-57.328 -29.880
-57.295 -29.832
-57.242 -29.789
-57.227 -29.780
-57.171 -29.781
-57.134 -29.771

我正在使用mac os 10.6.7 &活跃的Python 2.7.1

I'm trying to draw a map with pyqt and it does not work. So far either I have no output or I get errors like Seg fault.

Here is the code I'm using now:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(0, 0, 500, 500)
        self.setWindowTitle('Painel')
        list_ = []
        file_ = open('points.txt')
        for line in file_.readlines():
            l = line.replace("\n", "")
            l = l.split(" ")
            try:
                l = [float(i) for i in l]
                list_.append(l)
            except: pass#possible strings
        first = list_[0]
        list_ = list_[1:]
        self.path = QPainterPath()
        self.path.moveTo(*first)
        for i in list_:
            self.path.lineTo(*i)

    def paintEvent(self, e):      
        qp = QPainter()

        qp.begin(self)
        qp.drawPath(self.path)
        qp.end()


app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()

[Edit] And here is some of the content of points.txt

-57.328 -29.972
-57.323 -29.937
-57.329 -29.895
-57.328 -29.880
-57.295 -29.832
-57.242 -29.789
-57.227 -29.780
-57.171 -29.781
-57.134 -29.771

And I'm using mac os 10.6.7 & active python 2.7.1

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

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

发布评论

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

评论(1

生生漫 2024-11-14 10:57:39

我在旧的 Debian 稳定版上使用 Python 2.6.6。

您需要抵消负数以使它们变为正数,否则它们将呈现“屏幕外”并且在您的应用程序中不可见。

I'm using Python 2.6.6 on old Debian stable.

You'll need to offset negative numbers to make them positive, or they'll render "offscreen" and won't be visible in your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文