python - 获取目录后更新文件路径
问题描述
我是 python 新手,正在尝试创建一个需要输入和输出目录的 GUI。My goal is to have a textbox new to a browse button and when the directory is selected the textbox will display the directory path. 但是,我不确定如何更改在函数中单击按钮时显示的文本。
from tkinter import *
from tkinter import filedialog
import tkinter as tk
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
T_out= tk.Text(root, height=1, width=50)
T_out.pack(side=TOP)
self.hi_there= Button(frame, text="Browse", command=self.getoutputdir)
self.hi_there.pack(side=LEFT)
T_in = tk.Text(root, height=1, width=50)
T_in.pack(side=LEFT)
self.input_1= Button(frame, text="Browse", command=self.getinputdir)
self.input_1.pack(side=LEFT)
def getoutputdir(self):
global outputdir
outputdir = filedialog.askdirectory(parent=root,initialdir="/",title='Please select the output directory')
T_out.text(tk.END,outputdir)
def getinputdir(self):
global inputdir
inputdir = filedialog.askdirectory(parent=root,initialdir="/",title='Please select the input directory')
T_in.text(tk.END,inputdir)
root = Tk()
root.title('GUI for CZI')
app = App(root)
root.mainloop()
解决方案
有两个主要问题阻止了它的工作。首先是在getinputdir
andgetoutputdir
函数中。您需要确保对T_in
和的引用T_out
可用。您可以通过将它们存储在 App 对象中来做到这一点。
第二个主要问题是.text
不是一个有效的方法。您可以使用.delete
清除它然后.insert
插入新目录。
from tkinter import *
from tkinter import filedialog
import tkinter as tk
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.T_out= tk.Text(root, height=1, width=50)
self.T_out.pack(side=TOP)
self.hi_there= Button(frame, text="Browse", command=self.getoutputdir)
self.hi_there.pack(side=LEFT)
self.T_in = tk.Text(root, height=1, width=50)
self.T_in.pack(side=LEFT)
self.input_1= Button(frame, text="Browse", command=self.getinputdir)
self.input_1.pack(side=LEFT)
def getoutputdir(self):
global outputdir
outputdir = filedialog.askdirectory(parent=root,initialdir="/",title='Please select the output directory')
self.T_out.delete(1.0, tk.END)
self.T_out.insert(tk.END, outputdir)
def getinputdir(self):
global inputdir
inputdir = filedialog.askdirectory(parent=root,initialdir="/",title='Please select the input directory')
self.T_in.delete(1.0, tk.END)
self.T_in.insert(tk.END, inputdir)
root = Tk()
root.title('GUI for CZI')
app = App(root)
root.mainloop()
推荐阅读
- google-sheets - Google 表格 - 创建过去 90 天平均收盘价的折线图?
- angular - 按钮路由器链接不起作用
- javascript - 我使用了一个表达式来减少 javascript 中的 for 循环,但它不起作用
- python - 使用底图、Python 散点图坐标数据,
- typescript - 打字稿:将通用返回为字符串
- mysql - mysql 服务在 ubuntu 18.04 中自动重启
- sql - (SQL 合并)我在表中得到重复项
- excel - Excel VBA 和带引号的公式
- tensorflow - Python 文件运行不正常 / TensorFlow
- visual-studio-code - VS Code:单一键绑定到 Spit/Merge 编辑器?