绘制 PyQt 绘图器

2 投票
1 回答
1775 浏览
提问于 2025-04-16 17:39

我正在尝试用pyqt画一个地图,但一直没有成功。到目前为止,要么什么都没有输出,要么就出现像“段错误”这样的错误。

这是我现在使用的代码:

#!/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和active python 2.7.1

1 个回答

2

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

你需要把负数转换成正数,不然它们会显示在屏幕外面,你的应用就看不到这些数字了。

撰写回答