【发布时间】:2017-02-15 01:27:02
【问题描述】:
我正在尝试在 tkinter 中创建一个浏览按钮。我已经创建了打开文件夹对话框,但是当我将它设置为按钮时,它会退出窗口。
我的最终目标是: 1)点击按钮,弹出文件对话框 2) 选择一个文件 3) 将文件名插入到 Entry Widget 中以备后用
我应该注意我使用了多个窗口框架来总结下面的代码。
import os
import sys
import Tkinter as tk
from tkFileDialog import askopenfilename
def openFile(entryWidgetName):
tk.Tk().withdraw()
filename = askopenfilename()
entryWidgetName.delete(0,tk.END)
entryWidgetName.insert(0,filename)
return
class Welcome():
def __init__(self,master):
self.buttonNewTemplate = tk.Button(self.master, text = 'Create a New Template', command = self.gotoNewTemplate).place(x=100, y=250)
def gotoNewTemplate(self):
root2 = tk.Toplevel(self.master)
newTemplate = NewTemplate(root2)
class NewTemplate():
def __init__(self, master):
#Entry Windows
self.uploadFile = tk.Entry(self.sectionFrame2, width = 80).grid(row=4, column = 1, sticky = 'w')
#Buttons
self.buttonBrowse=tk.Button(self.sectionFrame2, text='Browse', fg='blue', command=lambda:openFile(uploadFile)).grid(row=4, column = 0, padx = 10, sticky = 'w')
每次我单击浏览按钮时,第二个窗口都会自行销毁,将我带回主页。
有人有什么建议吗?
【问题讨论】:
标签: python python-2.7 tkinter