Tkinter gui图形 [英] Tkinter gui graph

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

问题描述

我正在寻找有关如何在tkinter中为我已经在python中编程的图形创建gui的信息的指导.我想做的是构建一个可以导入csv数据的gui,然后用户单击一个按钮,该按钮将显示他们想要的图形类型.现在我有4个用python创建的图形,我不确定如何将它们转换为tkinter格式.我对 python 和 tkinter 很陌生.任何指导将不胜感激.这是我迄今为止为 tkinter 编写的代码.

I am looking for some guidance on where to look up some information on creating a gui in tkinter for graphs that I have already program in python. What I would like to do is to build a gui that can import csv data and then the user would click a button that would display the type of graph they would want. Right now I have 4 graphs that I have created in python and I am not sure how to bring them over to tkinter format. I am very new to python and tkinter. Any guidance would be greatly appreciated. Here is the code I have made so far for tkinter.

import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter import *
import pandas as pd
import subprocess
import webbrowser
import sys

def import_csv_data():
    global v
    csv_file_path = askopenfilename()
    print(csv_file_path)
    v.set(csv_file_path)
    df = pd.read_csv(csv_file_path)

root = tk.Tk()
tk.Label(root, text='File Path').grid(row=0, column=0)
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set',command=import_csv_data).grid(row=1, column=0)
tk.Button(root, text='Close',command=root.destroy).grid(row=1, column=1)

tk.Button(root, text='Graph 1', command=doNothing).grid(row=3, column=0)
tk.Button(root, text='Graph 2', command=doNothing).grid(row=3, column=1)
tk.Button(root, text='Graph 3', command=doNothing).grid(row=3, column=2)
tk.Button(root, text='Graph 4', command=doNothing).grid(row=3, column=3)



def doNothing():
    print("nothing")

def create_window():
    window = tk.Tk()    


menu =  Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File",menu=subMenu)
subMenu.add_command(label="New", command=create_window)
subMenu.add_command(label="Open", command=doNothing)
subMenu.add_command(label="Restart", command=doNothing)
subMenu.add_command(label="Exit", command=doNothing)
editMenu = Menu(menu)
menu.add_cascade(label = "Help", menu=editMenu)
editMenu.add_command(label="Help", command=doNothing)



root.mainloop()

这是我为其中一张图表编写的代码

Here is the code I have written for 1 of my graphs

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('csv.data')

# Indicated your x values and y values. 
x = df["X Data"]
y1 = df["Y1 Data"]
y2 = df["Y2 Data"]
z = df["Y3 Data"]
y_pos = np.arange(len(x))


lns1 = plt.bar(y_pos,z)
plt.ylabel('Bar Graph')
plt.xlabel('Date')


plt.twinx()
lns2 = plt.plot(y_pos,y1,'r-',linewidth=2.5)
lns3 = plt.plot(y_pos,y2,color='orange',linewidth=2.5)
plt.ylabel('Line Data')
plt.xticks(y_pos, x)
plt.xlabel('X axis')
plt.title('Graph 1')

plt.legend([lns1, lns2[0], lns3[0]],["Bar", "Line 1", "Line 2"], loc="upper right")

plt.draw()
plt.show()

推荐答案

这是一种方法(您没有说要在 tkinter 窗口中显示图形,所以我假设图形将显示在单独的matplotlib 窗口):

Here is one way of doing it (You did not say you want to display the graph in the tkinter window so I assume the graph will be displayed in a separate matplotlib window):

  1. 首先,将图形的代码放入函数中,以便您可以调用它们当你想要的时候.我将提供的代码放在一个名为 display_graph ,它将 csv 文件作为参数.然后我将该模块保存为 graph1.py.
  1. First, put the code for the graphs in functions so you can call them when you want. I put the provided code in a function called display_graph, which takes the csv file as an argument. I then saved that module as graph1.py.

这是graph1.py代码:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Put the code in a function so you cal call it later
def display_graph(data):
    df = pd.read_csv(data)

    # Indicated your x values and y values. 
    x = df["X Data"]
    y1 = df["Y1 Data"]
    y2 = df["Y2 Data"]
    z = df["Y3 Data"]
    y_pos = np.arange(len(x))

    lns1 = plt.bar(y_pos,z)
    plt.ylabel('Bar Graph')
    plt.xlabel('Date')

    plt.twinx()
    lns2 = plt.plot(y_pos,y1,'r-',linewidth=2.5)
    lns3 = plt.plot(y_pos,y2,color='orange',linewidth=2.5)
    plt.ylabel('Line Data')
    plt.xticks(y_pos, x)
    plt.xlabel('X axis')
    plt.title('Graph 1')

    plt.legend([lns1, lns2[0], lns3[0]],["Bar", "Line 1", "Line 2"], loc="upper right")

    plt.draw()
    plt.show()

#display_graph('data.csv')

  1. 然后使用以下命令将 graph1.py 模块导入到tkinter gui文件中导入graph1
  2. 定义按钮命令的功能.我定义了 graph_1 并从 graph1 模块中调用了 display_graph.
  3. 最后,我将Graph 1"按钮的命令更改为 graph_1.
  1. Then import the graph1.py module into the tkinter gui file using import graph1
  2. Define a function for the button command. I defined graph_1 and called display_graph from graph1 module in it.
  3. Finally, I changed the command for 'Graph 1' button to graph_1.

这是tkinter gui的代码:

Here is the code for the tkinter gui:

注意::我使用的是python 2.7,所以我更改了一些import语句,您必须将它们更改回与python 3等效.

Note: I'm using python 2.7 so I changed some of the import statements, you'll have to change them back to the python 3 equivalent.

#import Tkinter as tk
#from tkFileDialog import askopenfilename
#from Tkinter import *
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter import *
import pandas as pd
import subprocess
import webbrowser
import sys

import graph1 # import the graph1 module

def import_csv_data():
    global v
    csv_file_path = askopenfilename()
    print(csv_file_path)
    v.set(csv_file_path)
    df = pd.read_csv(csv_file_path)

# Define the functions before calling them
def doNothing():
    print("nothing")

def create_window():
    window = tk.Tk() 

# Define a function for 'Graph 1' button. This just calls the 'display_graph' function from 
# the 'graph1' module.
## You could avoid defining this function and use lambda and graph1.display_graph(v.get())
## in the 'Graph 1' button command but I prefer it this way.
def graph_1():
    graph1.display_graph(v.get())


root = tk.Tk()
tk.Label(root, text='File Path').grid(row=0, column=0)
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set',command=import_csv_data).grid(row=1, column=0)
tk.Button(root, text='Close',command=root.destroy).grid(row=1, column=1)

tk.Button(root, text='Graph 1', command=graph_1).grid(row=3, column=0) # Call the graph_1 function
tk.Button(root, text='Graph 2', command=doNothing).grid(row=3, column=1)
tk.Button(root, text='Graph 3', command=doNothing).grid(row=3, column=2)
tk.Button(root, text='Graph 4', command=doNothing).grid(row=3, column=3)


menu =  Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File",menu=subMenu)
subMenu.add_command(label="New", command=create_window)
subMenu.add_command(label="Open", command=doNothing)
subMenu.add_command(label="Restart", command=doNothing)
subMenu.add_command(label="Exit", command=doNothing)
editMenu = Menu(menu)
menu.add_cascade(label = "Help", menu=editMenu)
editMenu.add_command(label="Help", command=doNothing)

root.mainloop()

这是我运行 gui 脚本时的输出,浏览 csv 文件并单击Graph 1"按钮:

This is the output when I run the gui script, browse for a csv file and click on the 'Graph 1' button:

csv文件示例

X Data,Y1 Data,Y2 Data,Y3 Data
0,5,15,100
2,6,30,125
4,4,20,122
6,10,45,128
8,15,10,79
10,14,10,84
13,20,12,99
14,6,13,56
16,4,18,67
18,8,25,83
20,9,12,91

Tkinter 界面

Matplotlib图

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

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