Можем ли мы использовать matplotlib.animation в kivy?Если да, то как? - PullRequest
0 голосов
/ 18 декабря 2018

Ну, это Как использовать анимацию matplotlib в kivy Вопрос похож, но ответ просто не имеет никакого значения. Также, как указал @ImportanceOfBeingErnest, я отредактировал вопрос: Можем ли мы это сделать? Если да, топерейдите ниже.

Так что было легко добавить анимацию в tkinter согласно следующему уроку Учебник Sentdex о том, как добавить анимацию matplotlib в tkinter

Я пытался сделатьто же самое в киве, но не могу понять, где написать строку ani=animation.Funcanimation(blabla) См. последнюю строку в следующем коде

class Graph(FigureCanvasKivyAgg):
    def __init__(self,*args,**kwargs):
        FigureCanvasKivyAgg.__init__(self,f)
        pullData = open("TimeLatitude.txt", "r").read()
        dataList = pullData.split('\n')
        xList = []
        yList = []
        pullData2 = open("TimeLongitude.txt", "r").read()
        dataList2 = pullData2.split('\n')
        xList2 = []
        yList2 = []
        for eachLine in dataList:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList.append(int(x))
                yList.append(int(y))
        for eachLine in dataList2:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList2.append(int(x))
                yList2.append(int(y))
        a.clear()
        a.plot(xList, yList, "#00A3E0", label="Latitude")
        a.plot(xList2, yList2, "#183A54", label="Longitude")
        a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
                 ncol=2, borderaxespad=0)
        title = "Oxymora Mars Rover Geographic Points\nLast Longitude : " + str(
            yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
        a.set_title(title)
        ani = animation.FuncAnimation(f, animate, interval=1000)

1 Ответ

0 голосов
/ 20 декабря 2018

Я думаю, что в наше время kivy используется меньше. Никто не ответил. Поэтому я каким-то образом нашел альтернативу тому, чего я хотел достичь.Я добавил кнопку «Обновить», которая снова запускает всю функцию, которая включает выборку значений из файла и снова рисует график. Таким образом, всякий раз, когда значения обновляются в файле, мы получаем график для этого.

Воткод python 3.

class Graph(FigureCanvasKivyAgg):
    def __init__(self,*args,**kwargs):
        FigureCanvasKivyAgg.__init__(self,f)
        pullData = open("TimeLatitude.txt", "r").read()
        dataList = pullData.split('\n')
        xList = []
        yList = []
        pullData2 = open("TimeLongitude.txt", "r").read()
        dataList2 = pullData2.split('\n')
        xList2 = []
        yList2 = []
        for eachLine in dataList:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList.append(int(x))
                yList.append(int(y))
        for eachLine in dataList2:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList2.append(int(x))
                yList2.append(int(y))
        a.plot(xList, yList, "#00A3E0", label="Latitude")
        a.plot(xList2, yList2, "#183A54", label="Longitude")
        a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
             ncol=2, borderaxespad=0)
        title = "Oxymora Mars Rover Coordinates\nLast Longitude : " + str(
            yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
        a.set_title(title)
    def animate(self):
        pullData = open("TimeLatitude.txt", "r").read()
        dataList = pullData.split('\n')
        xList = []
        yList = []
        pullData2 = open("TimeLongitude.txt", "r").read()
        dataList2 = pullData2.split('\n')
        xList2 = []
        yList2 = []
        for eachLine in dataList:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList.append(int(x))
                yList.append(int(y))
        for eachLine in dataList2:
            if len(eachLine) > 1:
                x, y = eachLine.split(',')
                xList2.append(int(x))
                yList2.append(int(y))
        a.clear()
        a.plot(xList, yList, "#00A3E0", label="Latitude")
        a.plot(xList2, yList2, "#183A54", label="Longitude")
        a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
                 ncol=2, borderaxespad=0)
        title = "Oxymora Mars Rover Coordinates\nLast Longitude : " + str(
            yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
        a.set_title(title)
        self.draw()

и вот соответствующий код kivy.

<MainScreen>:
    name: "main"
    FloatLayout:
        Graph
            id:gr
        Button:
            on_release: gr.animate()
            text: "Refresh"
            font_size: 15
            size_hint:0.068,0.05
            pos_hint: {"x":0,"top":0.8}
            color: 1,0,1,1

Итак, наконец, после запуска моего полного кода приложение kivy выглядит так - Изображение приложения Kivy

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...