我需要添加一个用户按下的按钮来启动Tkinter.askdirectory,并能够捕获结果。我已经尝试了很多例子,但是在我的特定情况下找不到任何可以成功运行的东西(我是新手并不知道我在做什么的情况:)
我已经评论了我所做的事情,因此原始代码仍然有用,所以你可以看看我一直在尝试什么。由于我与Tk在一个不熟悉的领域,这大部分都不起作用。非常感谢任何帮助。
#!/usr/bin/env python
import Tkinter
from Tkinter import *
import Tkinter, tkFileDialog
class Values(Tkinter.Tk):
"""docstring for Values"""
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
stepOne = Tkinter.LabelFrame(self, text=" Create A Playlist ")
stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
# self.getAdir = tkFileDialog.askdirectory(parent=stepOne, title='Please select a directory')
self.Val1Lbl = Tkinter.Label(stepOne,text="Playlist Name")
self.Val1Lbl.grid(row=0, column=0, sticky='E', padx=10, pady=2)
self.Val1Txt = Tkinter.Entry(stepOne)
self.Val1Txt.grid(row=0, column=1, columnspan=4, pady=2, sticky='WE')
self.Val2Lbl = Tkinter.Label(stepOne,text="Task")
self.Val2Lbl.grid(row=1, column=0, sticky='E', padx=10, pady=2)
self.Val2Var = StringVar()
self.Val2Txt = Tkinter.OptionMenu(stepOne, self.Val2Var, 'Layout','Anim Pass 1','Anim Pass 2', 'Lighting', 'Compositing')
self.Val2Txt.grid(row=1, column=1, columnspan=4, pady=2, sticky='WE')
self.Val3Lbl = Tkinter.Label(stepOne,text="Description")
self.Val3Lbl.grid(row=2, column=0, sticky='E', padx=10, pady=2)
self.Val3Txt = Tkinter.Entry(stepOne)
self.Val3Txt.grid(row=2, column=1, columnspan=4, pady=2, sticky='WE')
# self.Val4Lbl = Tkinter.Label(stepOne,text="Directory")
# self.Val4Lbl.grid(row=3, column=0, sticky='E', padx=10, pady=2)
# self.Val4Var = StringVar()
# self.Val4Var = Tkinter.Button(command=getAdir)
# self.Val4Txt.grid(row=3, column=1, columnspan=4, pady=2, sticky='WE')
self.val1 = None
self.val2 = None
self.val3 = None
# self.val4 = None
SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)
def submit(self):
self.val1=self.Val1Txt.get()
if self.val1=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.val2=self.Val2Var.get()
if self.val2=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.val3=self.Val3Txt.get()
if self.val3=="":
Win3=Tkinter.Tk()
Win3.withdraw()
# self.val4=self.Val4Var.get()
# if self.val4=="":
# Win4=Tkinter.Tk()
# Win4.withdraw()
self.quit()
if __name__ == '__main__':
app = Values(None)
app.title('Bulk Movie Upload')
app.mainloop() #this will run until it closes
#Print the stuff you want.
print app.val1,app.val2,app.val3 #,app.val4
这是修改后的代码,从我认为你的意思,但仍然有错误,因为它没有传递变量。我编辑了这篇文章和代码,以简化。
#!/usr/bin/env python
import Tkinter
from Tkinter import *
import Tkinter, tkFileDialog
class Values(Tkinter.Tk):
"""docstring for Values"""
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def getAdir(self):
self.val5 = tkFileDialog.askdirectory(parent=self, title='Please select a directory')
def initialize(self):
self.grid()
stepOne = Tkinter.LabelFrame(self, text=" Create A Playlist ")
stepOne.grid(row=1, columnspan=8, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
self.Val5Lbl = Tkinter.Label(stepOne,text="Select Folder Containing Movies")
self.Val5Lbl.grid(row=4, column=0, sticky='E', padx=5, pady=2)
self.Val5Var = StringVar()
self.Val5Txt = Tkinter.Button(stepOne, text="Select Files", command=self.getAdir)
self.Val5Txt.grid(row=4, column=1, columnspan=4, pady=2, sticky='WE')
self.val5 = None
SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
SubmitBtn.grid(row=6, column=3, sticky='W', padx=5, pady=2)
def submit(self):
self.val5=self.Val5Var.get()
if self.val5=="":
Win2=Tkinter.Tk()
Win2.withdraw()
self.quit()
if __name__ == '__main__':
app = Values(None)
app.title('Bulk Movie Upload')
app.mainloop() #this will run until it closes
#Print the stuff you want.
print "Folder is - " + app.val5
答案 0 :(得分:1)
一个大问题是您正在创建多个Tk
实例。你不能这样做。如果您需要更多窗口,请在Toplevel
创建实例。
要调用函数来请求目录,您需要创建一个函数,并将该函数与按钮联系起来。你在做什么实际上是在创建其他小部件之前调用该函数,而不是等待按下按钮。
def getAdir(self):
self.val4 = tkFileDialog.askdirectory(parent=stepOne, title='Please select a directory')
...
self.Val4Var = Tkinter.Button(self, command=self.getAdir)
注意:在我写完上述内容后,问题被更改为包含不同的代码。以下是我对该代码的评论:
在修改后的代码中,您没有按照我在答案中建议的那样做。您需要删除这行代码:
self.val5=self.Val5Var.get()
这是因为self.val5
已经包含从askdirectory
返回的目录。它将是一个字符串,而不是某种具有get
方法的对象。
您还会遇到打开多个根窗口的问题。这将导致其他问题。