【问题标题】:Updating file path upon getting directory获取目录时更新文件路径
【发布时间】:2019-07-10 13:56:59
【问题描述】:

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

【问题讨论】:

标签: python tkinter


【解决方案1】:

有两个主要问题阻止了它的工作。第一个是在getinputdirgetoutputdir 函数中。您需要确保对T_inT_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()

【讨论】:

  • 谢谢!真的很感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2014-04-21
  • 2015-12-07
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
相关资源
最近更新 更多