2017-06-23 11 views
0

私は既にpythonでプログラムしているグラフのtkinterでguiを作成するための情報をどこで調べるべきかに関するガイダンスを探しています。私がしたいのは、csvデータをインポートできるguiを構築し、ユーザが望むグラフのタイプを表示するボタンをクリックすることです。今私は、私がPythonで作成した4つのグラフを持っており、tkinter形式にそれらを引き渡す方法がわかりません。私はpythonとtkinterにはとても新しいです。どんな指針も大変ありがとうございます。 ここまではtkinterのために私が作ったコードです。ここでTkinter GUIグラフ

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() 

私はここに私のグラフの1

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() 

答えて

0

のために書かれているコードは、それを行うための一つの方法です(あなたはあなたがTkinterをウィンドウにグラフを表示したいとしていなかった私)グラフは別のmatplotlibのウィンドウに表示されますと仮定します。

  1. まず、お好きな時に、あなたが それらを呼び出すことができるような機能のグラフのためのコードを置きます。提供されたコードを display_graphという名前の関数に入れました。これは、ファイルを引数としてcsvをとります。私は そのモジュールを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') 
  • そしてボタンの機能を定義 import graph1
  • を用いTkinterのGUIのファイルにgraph1.pyモジュールをインポートコマンド。私はgraph_1と定義し、display_graphgraph1モジュールと呼んでいます。
  • 最後に、「グラフ1」ボタンのコマンドをgraph_1に変更しました。ここで
  • のTkinterのGUIのコードです:

    注:はので、私はimport文の一部を変更し、あなたが戻ってのpythonにそれらを変更する必要があります私のpython 2.7を使用しています3等価。

    #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ファイルを参照し、 'グラフ1' ボタンをクリックし、出力されます。

    サンプル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 gui

    enter image description here

    matplotlibのグラフ

    enter image description here

    +0

    私は十分に感謝することはできません!これは美しく動作します! –