2016-11-07 68 views
0

我很新的python編程,這是我的第一個問題,所以請溫和我。現在我正在研究一種讀取CSV文件的tkinter GUI,將它們轉換爲數據幀以編輯它們,並將處理後的文件保存到數據路徑中的result.csv中。我的問題是,我想用一個連接到一個按鈕的函數OpenFile()讀取CSV,然後在我的「Editfunctions」中使用返回的數據框,每個按鈕都連接到一個按鈕。這似乎不起作用,我的代碼中回調。這裏是我的Tkinter GUI目前的樣子:如何交換tkinter按鈕之間的數據幀?

root = tk.Tk() 
openbutton = tk.Button (root, text='OpenFile', command = lambda: OpenFile()) 
openbutton.pack() 
brakebutton = tk.Button (root, text = 'EditFile', command = lambda: EditFile(z)) 
brakebutton.pack() 
savenewbutton = tk.Button(root, text = 'SavetoNewFile', command = lambda: SaveinNewFile(out_tup_end3)) 
savenewbutton.pack() 
saveexcbutton = tk.Button(root, text = 'AppendtoExistingFile', command = lambda: SavetoexistingFile(out_tup_end3)) 
saveexcbutton.pack() 
root.mainloop() 

MY功能基本上是這樣的:

def OpenFile(): 
     name = askopenfilename() 
     df = pd.read_csv(name, sep = ';') 
     return df 

def EditFile(z): 


     df1 = z[[]] 
. 
. 
. 
. 
return out_tup_end3 

def SaveinNewFile(out_tup_end3): 
    out_tup_end3.to_csv("datapath", sep = ';', index = 0, mode = 'w') 


def SavetoexistingFile(out_tup_end3):   
    with open("datapath", 'a') as f: 
     out_tup_end3.to_csv(f, header=False, sep = ';', index = 0) 


def main(): 

    z = OpenFile() 
    EditFile(z) 
    editedFile = EditFile(z) 
    SaveinNewFile(editedFile) 
    SavetoexistingFile(editedFile) 


if __name__ == "__main__": 
    main() 

所以我認爲不正常的是,回調不移交數據框讀從OpenFile()到EditFile(z)。我怎麼能做到這一點? 感謝您的幫助!

+0

我真的不明白這是什麼程序是應該做的,但我有一個想法;是否因爲z超出了範圍? – Grezzo

+0

我在EditFile(z)函數中忽略了一小段代碼,其中我使用從OpenFile()移交來的數據框進行遊戲。但這種交付似乎並不奏效。基本上我想擺脫錯誤,說:Tkinter回調異常..... NameError:名稱'z'未定義。 –

+0

它在'command = lambda:EditFile(z)'中進行接縫,嘗試使用只存在於main()中的'z'。您必須創建全局變量'z'(在所有函數之外)並使用'global z'函數內部。順便說一句:'command ='執行函數,但不關心返回值 - 所以'return df'沒用。 – furas

回答

0

command=執行功能,但它不關心從該函數返回的值,所以你必須使用global變量來使用其他函數中的一個函數的結果。

你的代碼看起來是這樣的

#!/usr/bin/env python3 

import tkinter as tk 
from tkinter.filedialog import askopenfilename 
from tkinter.messagebox import showinfo, showwarning 
import pandas as pd 

# --- functions --- 

def open_file(): 
    # inform function to use global variables 
    global df 

    name = askopenfilename() 

    if name: 
     df = pd.read_csv(name, sep=';') 
     showinfo("INFO", "DataFrame created") 

def edit_file(): 
    # inform function to use global variables 
    global df, out_tup_end3 

    if df is None: 
     #print("Read file first") 
     showwarning("WARNING", "Read file first") 
    else: 
     df1 = df[[]] 

     # ... 

     out_tup_end3 = df1 
     showinfo("INFO", "DataFrame edited") 


def save_in_new_file(): 
    # inform function to use global variables 
    global out_tup_end3 

    if out_tup_end3 is None: 
     #print("Read file first") 
     showwarning("WARNING", "Read file first") 
    else: 
     out_tup_end3.to_csv("datapath", sep=';', index=0, mode='w') 
     showinfo("INFO", "DataFrame saved") 


def save_to_existing_file():   
    # inform function to use global variables 
    global out_tup_end3 

    if out_tup_end3 is None: 
     #print("Read file first") 
     showwarning("WARNING", "Read file first") 
    else: 
     with open("datapath", 'a') as f: 
      out_tup_end3.to_csv(f, header=False, sep=';', index=0) 
     showinfo("INFO", "DataFrame appended") 


# --- main --- 

if __name__ == "__main__": 

    # create global variables 

    df = None 
    out_tup_end3 = None 

    # GUI 

    root = tk.Tk() 

    b = tk.Button(root, text='Open File', command=open_file) 
    b.pack(fill='x') 

    b = tk.Button(root, text='Edit File', command=edit_file) 
    b.pack(fill='x') 

    b = tk.Button(root, text='Save to New File', command=save_in_new_file) 
    b.pack(fill='x') 

    b = tk.Button(root, text='Append to Existing File', command=save_to_existing_file) 
    b.pack(fill='x') 

    root.mainloop() 

或者學習如何使用類和self.代替global

+0

非常感謝!這有很大的幫助,我會毫不猶豫地看看如何使用類。但是現在這工作得很好。 –