如何创建一个访问tkinter中文件的按钮?

时间:2016-07-28 03:48:25

标签: python tkinter python-3.5

我一直想制作一个允许我打开目录窗口并从文件夹中选择csv文件的代码。我想制作4个按钮来做那个,然后按下一个按钮,如果按下它会运行一个代码并写一个新文件。

我已经尝试了几种方法,但到目前为止我并没有太多这是我的代码:

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)

1 个答案:

答案 0 :(得分:1)

这些方面的东西:

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

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

通过使用command=getFile,Tk知道在单击按钮时调用getFile方法。

祝你好运!