在 Python GUI 中创建图形 [英] Creating Graphs in Python GUI

查看:21
本文介绍了在 Python GUI 中创建图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 GUI,我可以在其中读取 CSV 文件并根据数据计算液体输出.现在我想做两件事:
1)我想根据时间生成输出,就像日期
2) 我想在我的 GUI 中的单独窗口上为用户特定的时间或日期生成图形

I have created a GUI in which I read a CSV file and calculate the liquid output from the data. Now I want to do two things:
1) I want to generate the output based on time just like date
2) I want to generate graphs on a separate window in my GUI for a user specific time or date

这是我的代码:

import csv
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo
import datetime

#csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv"))
from Tools.scripts.treesync import raw_input
class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)


        button1 = Button(self, text="Browse for a file", command=self.askfilename)
        button2 = Button(self, text="Count the file", command=self.takedate)
        button3 = Button(self, text="Exit", command=master.destroy)
        button1.grid()
        button2.grid()
        button3.grid()
        self.userInputFromRaw = Entry(self)
        self.userInputFromRaw.grid()

        self.userInputToRaw = Entry(self)
        self.userInputToRaw.grid()

        self.grid()

    def askfilename(self):
        in_file = askopenfilename()
        if not in_file.endswith(('.CSV')):
            showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?')
        else:
            self.in_file=in_file

    def CsvImport(self,csv_file):


        dist = 0
        for row in csv_file:
            _dist = row[0]
            try:
                _dist = float(_dist)
            except ValueError:
                _dist = 0

            dist += _dist
        print ("Urine Volume is: %.2f" % (_dist*0.05))


    def takedate(self):
        from_raw = self.userInputFromRaw.get()
        from_date = datetime.date(*map(int, from_raw.split('/')))
        print ('From date: = ' + str(from_date))
        to_raw = self.userInputToRaw.get()
        to_date = datetime.date(*map(int, to_raw.split('/')))
        in_file = ("H:\DataLog.csv")
        in_file= csv.reader(open(in_file,"r"))

        for line in in_file:
            _dist = line[0]
            try:
                file_date =  datetime.date(*map(int, line[1].split(' ')[1].split('/')))
                if from_date <= file_date <= to_date:
                    self.CsvImport(in_file)

            except IndexError:
                pass

root = Tk()
root.title("Urine Measurement")
root.geometry("500x500")
app = App(root)
root.mainloop()

我怎样才能完成上述 2 个任务?

How can I achieve the above 2 tasks?

推荐答案

我必须同意 Jacques de Hooge 的观点,您应该为此使用 matplotlib.在文件开始时,导入它:

I must agree with Jacques de Hooge, you should be using matplotlib for that. At the beggining of your file, import it:

import matplotlib.pyplot as plt

因为你只想打开一个带有绘图的新窗口,一个 matplotlib 窗口就足够了.您可以使用散点图:

As you only want to open a new window with the plot, a matplotlib window should suffice. You can use a scatter plot:

plt.scatter(X, Y)

其中 X 是具有 x 坐标的可迭代对象,而 Y 是具有 y 坐标的可迭代对象.由于您想要做一些随时间不断变化的事情,您可以使用 values 列表和要绘制的值,执行以下操作:

Where X is an iteratable with the x coordinates and Y an iteratable with the y coordinates. Since you want to do something with a constant change in time, you can, having a values list with the values to plot, do the following:

plt.scatter(range(len(values)), values)
plt.show()

您可能还想在一个线程中运行它,以便在 matplotlib 窗口打开时程序的其余部分不会冻结".有很多地方对此进行了解释.

You might also want to run this inside a thread, so that the rest of the program doesn't "freeze" while the matplotlib window is open. There are plenty of places where this is explained.

这篇关于在 Python GUI 中创建图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆