使用浏览文件中的文本更新文本框.Python [英] Update textbox with text from browsed file. python

查看:49
本文介绍了使用浏览文件中的文本更新文本框.Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 Python 2.7 和 Tkinter.我有一个按钮可以浏览我的目录并获取文件的目录位置并将其保存到 filename.我希望在选择文件时自动将 inputBox 的值更改为 filename 的值.

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( )

附注.我对 Python 很陌生,任何建设性的批评将不胜感激.

PS. I am quite new to Python and any constructive criticism would be appreciated.

推荐答案

您可以使用 插入 方法.

def browse_directory():
    filename = tkFileDialog.askopenfilename()

    print(filename)

    inputBox.configure(state=NORMAL)
    inputBox.delete(0, "end")
    inputBox.insert(0, filename)
    inputBox.configure(state=DISABLED)

这篇关于使用浏览文件中的文本更新文本框.Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆