问题
I am currently using Python 2.7 and Tkinter. I have a button that browses my directory and takes the file's directory location and saves it to filename
. I would like this to change the value of inputBox
to the value of filename
automatically when the file is chosen.
import os
from Tkinter import *
import tkFileDialog
root = Tk()
root.title("Doc Word Frequency")
root.geometry("600x300")
def close_window ():
root.destroy()
def browse_directory():
filename = tkFileDialog.askopenfilename()
print(filename)
#Change value of inputBox
inputBox = Entry(root, width = 50)
inputBox.grid(row = 0, column = 0, padx = 20, pady = 20)
inputBox.insert(END, '"Upload Document File"')
inputBox.config(state = DISABLED)
Button(root, width = 9, text = 'Browse', command = browse_directory).grid(row = 0, column = 1, sticky = W, padx = 4)
Button(root, width = 9, text = 'Upload').grid(row = 0, column = 2, sticky = W, padx = 4)
Button(root, width = 9, text = 'Quit', command = close_window).grid(row = 0, column = 3, sticky = W, padx = 4)
mainloop( )
PS. I am quite new to Python and any constructive criticism would be appreciated.
回答1:
You can insert text into an entry widget with the insert method.
def browse_directory():
filename = tkFileDialog.askopenfilename()
print(filename)
inputBox.configure(state=NORMAL)
inputBox.delete(0, "end")
inputBox.insert(0, filename)
inputBox.configure(state=DISABLED)
来源:https://stackoverflow.com/questions/33047225/update-textbox-with-text-from-browsed-file-python