2016-07-28 20 views
-2

我一直想做一個代碼,讓我打開一個目錄窗口,並從文件夾中選擇一個csv文件。我想製作4個按鈕來做到這一點,然後一個按鈕,如果按下它將運行一個代碼並寫入一個新文件。如何製作一個訪問tkinter文件的按鈕?

我已經嘗試過多種方法,但到目前爲止,我得到了不多,這是我的代碼:

from tkinter import* 



#how to organize layout# 
root = Tk() #CONSTRUCTOR(think blank window in your head) 

topFrame = Frame(root) #this is pretty much saying, 
        # "i'm gonna make an invisible container and is gonna go into themain window, 
        # root". 

topFrame.pack()  #everytime there is something to display we have to pack it in. 

#Do the exact same thing for the bottom frame 

bottomFrame = Frame(root) 
bottomFrame.pack(side=BOTTOM) 

#let's through some widgets in here 

button1 = Button(topFrame,text="Button1",fg="yellow") 
button2 = Button(topFrame,text="Button2",fg="blue") 
button3 = Button(topFrame,text="Button3",fg="red") 
button4 = Button(topFrame,text="Button4",fg="white") 
button5 = Button(bottomFrame,text="Button5",fg="black") 

button1.pack(side=LEFT) 
button2.pack(side=LEFT) 
button3.pack(side=LEFT) 
button4.pack(side=LEFT) 
button5.pack(side=BOTTOM) 
+0

'tkinter.filedialog.askdirectory'可能是你要找的東西 –

回答

1

這些方針的東西:

from Tkinter import * 
def getFile(): 
    # Get File Code 

b = Button(text="click me", command=getFile) 
b.pack() 

通過使用command=getFile,TK知道調用單擊按鈕時的getFile方法。

祝你好運!