我如何修复此函数以使其读取用户传递的字符串作为路径而不是 python 上的其他内容? tkinter 相关

How can I fix this function to make it read a string passed by the user as a path and not as something else on python? tkinter related

我开发了一个程序,会向用户询问Chrome浏览器的Profile Path(可以通过在Chrome的地址栏中输入chrome://version/获得)浏览器),用户必须将该路径复制并粘贴到 data_input 条目中,然后单击 user_data 按钮继续该过程,代码如下:

import tkinter as tk 
from tkinter import ttk #for user inputs
from tkinter import messagebox #for warning messages 

root = tk.Tk()
root.geometry('500x400') #resolution
root.title("Bulkdozer") #Name of this program
root.attributes('-topmost', True) #keep the program's window top-most


def submit_profile_path():
    if len(profile_path.get()) == 0: #check if the user didn't type anything and pressed the button 
        messagebox.showerror(message="You didn't provide any input, try again", title="NULL Input")
    elif len(profile_path.get()) > 0:
        if r'\Google\Chrome\User Data' in profile_path.get() == True: #check if the path provided by the user is a valid one
            if profile_path.get().split("User Data\",1)[1] != None: #now check if at the end of that path exist an actual profile folder
                user_data.pack_forget() #hide the user_data button
                data_input.pack_forget() #hide the data_input
                profile_path_label.pack_forget() #hide the profile_path_label
                print(profile_path.get())
                open_browser.pack() #show the open_browser button
                
            else: #inform the user that he must provide the profile path containing the corresponding profile folder
                messagebox.showwarning(message="You forgot to add the profile folder in the profile path, try again", title="Profile Folder Missing")
                data_input.delete(0, tk.END)        
                
        else: #inform the user that he must provide a valid profile path
            messagebox.showwarning(message="The path provided does not seem to be the right one, try again", title="Invalid Profile PATH")
            data_input.delete(0, tk.END)
            
# BUTTON FOR PROVIDING THE PROFILE PATH OF CHROME BROWSER #
profile_path = tk.StringVar() #This variable will be used for storing the profile path string passed by the user
signin = ttk.Frame(root) #create a container for variable profile path
signin.pack(padx=55, pady=20, fill='x', expand=True) #define the dimensional measurement and location for this container
profile_path_label = ttk.Label(signin, text="Introduce YOUR profile path:") #create a label for the profile_path variable
profile_path_label.pack(fill='x', expand=True) #add the label
data_input = ttk.Entry(signin, textvariable=profile_path) #create an entry for the profile_path variable
data_input.pack(fill='x', expand=True)
data_input.focus()
user_data = tk.Button(root, width=20,  text="Submit User Data", command=submit_profile_path) #executes the function when clicked
user_data.place(x=60, y=40, width=100, height=30) #define the dimensional measurement and location for this button
user_data.pack() #Apply The Pack geometry manager to this button for using its functions later on

GUI 预览:

但是,它没有按预期工作,因为即使用户提供了完全正确的 Profile Path,在按下 user_data 按钮后,函数 submit_profile_path() 将始终执行条件 messagebox.showwarning(message="The path provided does not seem to be the right one, try again", title="Invalid Profile PATH"),如下所示:

其中不应该是因为我设置了条件if r'\Google\Chrome\User Data' in profile_path.get() == True:,意思是如果用户提供的路径包含\Google\Chrome\User Data,应该然后验证字符串 User Data\ 后是否有文件夹名称(在我的例子中是 Default),然后执行其余代码。

我想知道上面的代码还缺少什么才能按预期工作吗? (即执行条件if profile_path.get().split("User Data\",1)[1] != None:

  • 一般情况下,你通常不想比较== True除非在非常特殊的情况下

  • 在Python中,特别是r'\Google\Chrome\User Data' in profile_path.get() == True等同于r'\Google\Chrome\User Data' in profile_path.get() and profile_path.get() == True(除了.get()只被调用一次);这显然不是你想要的

要解决此问题,请省略 == True

if r'\Google\Chrome\User Data' in profile_path.get():