我是Python的新手,但本周开始编写一些小脚本。我目前正在尝试编写一个简单的程序来绘制一些数据。我想做以下事情:
我主要让程序运行 - 我可以使用tkFileDialog.askdirectory
选择目录,然后读取数据,制作图并使用鼠标点击它们。
我的问题在于使用tkFileDialog
打开的TK根窗口。如果我使用withdraw()
,则额外的窗口不会打开,但只会显示第一个图(鼠标单击会关闭该图,但不会显示下一个图)。如果我不使用withdraw()
,必须在第一个绘图之后手动关闭额外窗口以前进到第二个。
我想知道是否有办法选择避免显示额外窗口的目录?
我附上一些示例代码来展示我的思维过程。这不会调用实际数据但仍会重现问题(您需要将 .D 更改为目录中的某种文件类型):
import numpy as np
from pylab import *
import glob
import os
import Tkinter, tkFileDialog
##################################################
#define the mouse click event
##################################################
def moveon(event):
close()
##################################################
#ask for the directory
##################################################
root = Tkinter.Tk()
#root.withdraw()
direc = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
os.chdir(direc)
for files in glob.glob("*.D*"):
##################################################
#Read in the data
##################################################
#assume this reads x and y from each file
x = [1, 2]
y = [3, 4]
##################################################
#loop though the plots
##################################################
fig = figure(1)
plot(x,y)
cid = fig.canvas.mpl_connect('button_press_event',moveon)
show()
答案 0 :(得分:1)
由于您在文件对话框后似乎没有使用Tkinter,因此可以执行root.destroy()
在用户选择文件后立即关闭Tk根窗口。