【问题标题】:How can I get python tkinter to validate a file path via entry box?如何让 python tkinter 通过输入框验证文件路径?
【发布时间】:2021-07-27 16:36:28
【问题描述】:

我正在创建一个包含多行文件路径输入的表单,每个输入框旁边都有一个浏览按钮。用户应该能够将目录路径粘贴到输入表单中,并且如果它没有将背景更改为红色,它将在失去焦点时验证它是现有文件夹。

“浏览”按钮也可用于输入文件夹路径,当通过“浏览”按钮进行选择时,它会将所选路径输入到相应的输入框中。

这是我对 python 相当新的当前代码,并在 tkinter gui 上完成了 newb,因此尝试自学并寻求一些指导

import tkinter as tk
import tkinter.filedialog as fd
from pathlib import Path

root = tk.Tk()

root.geometry("500x500")
root.columnconfigure(0, weight = 1)

def browse_path():
    browse_path = fd.askdirectory()
    path.set(browse_path)
    
def enter_Path():
    entry_path = ent1.get()
    if Path(entry_path).is_dir():
        path.set(entry_path)
        return True
    else:
        ent1.config(bg="red")
        return False
        

label1 = tk.Label(root, text = "This is the Label").grid(row = 0)

path = tk.StringVar(root, value = "Enter the first path here")
ent1 = tk.Entry(root, textvariable = path, validate="focusout", validatecommand="enter_Path")
ent1.grid(padx = 5, pady = 5, row = 2, column = 0, sticky = tk.W + tk.E)

button1 = tk.Button(root, text = "Browse", command = browse_path).grid(row = 2, column = 1)

label2 = tk.Label(root, text = "This is the second label").grid(row = 3)

path2 = tk.StringVar(root, value = "Enter the second path here")
ent2 = tk.Entry(root, textvariable = path2)
ent2.grid(padx = 5, pady = 5, row = 4, column = 0, sticky = tk.W + tk.E)

button2 = tk.Button(root, text = "Browse", command = browse_path).grid(row = 4, column = 1)

root.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    据我所知,您可以这样做:

    1. 应该使用'<FocusOut>'。如果 GUI 窗口失去焦点,就会出现这种情况。
    2. os.path.isdir()。我对pathlib 不太熟悉。我已经导入了os 模块并使用os.path.isdir 来检查它是否是一个文件。

    这是一个示例程序:

    import tkinter as tk
    import tkinter.filedialog as fd
    import os
    
    root = tk.Tk()
    
    root.geometry("500x500")
    root.columnconfigure(0, weight = 1)
    
    def browse_path():
        browse_path = fd.askdirectory()
        path.set(browse_path)
    def browse_path_2():
        browse_path = fd.askdirectory()
        path2.set(browse_path)
        
    def enter_Path(event):
        entry_path = path.get()
        entry_path_2=path2.get()
        if not os.path.isdir(entry_path):
            ent1.config(bg="red")
        else:
            ent1.config(bg="white")
        if not os.path.isdir(entry_path_2):
            ent2.config(bg="red")
        else:
            ent2.config(bg="white")
                
    
    label1 = tk.Label(root, text = "This is the Label").grid(row = 0)
    
    path = tk.StringVar(root, value = "Enter the first path here")
    ent1 = tk.Entry(root, textvariable = path)
    ent1.grid(padx = 5, pady = 5, row = 2, column = 0, sticky = tk.W + tk.E)
    
    button1 = tk.Button(root, text = "Browse", command = browse_path).grid(row = 2, column = 1)
    
    label2 = tk.Label(root, text = "This is the second label").grid(row = 3)
    
    path2 = tk.StringVar(root, value = "Enter the second path here")
    ent2 = tk.Entry(root, textvariable = path2)
    ent2.grid(padx = 5, pady = 5, row = 4, column = 0, sticky = tk.W + tk.E)
    
    button2 = tk.Button(root, text = "Browse", command = browse_path_2).grid(row = 4, column = 1)
    root.bind("<FocusOut>",enter_Path)
    root.mainloop()
    

    【讨论】:

    • 这个答案的一部分包括一些错误的代码。 validatcommand 需要是命令,而不是字符串。
    • 谢谢你让我得到了我需要的东西。
    • @BryanOakley,我认为甚至不需要validatecommand
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 2017-03-30
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多