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
I developed a program that will ask the user for the Profile Path
of Chrome Browser (it can be obtained by typing chrome://version/
in the address bar of Chrome Browser), the user will have to copy and paste that path in to the data_input
entry, and then click on the user_data
button to continue with the process, here's the code:
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 preview:
However, it's not working as expected because even though the user provides exactly the right Profile Path
, after pressing the user_data
button, the function submit_profile_path()
will always execute the condition messagebox.showwarning(message="The path provided does not seem to be the right one, try again", title="Invalid Profile PATH")
as shown down below:
Which should not be the case because I set the condition if r'\Google\Chrome\User Data' in profile_path.get() == True:
, meaning that if the path provided by the user contains \Google\Chrome\User Data
, it should then verify that there's a folder name after the string User Data\\
(which in my case is Default), and then execute the rest of the code.
I would like to know what else is missing in my code above in order to work as expected? (i.e. execute the condition if profile_path.get().split("User Data\\",1)[1] != None:
)
Solution 1:
-
In general, you usually don't want to compare
== True
except in very special circumstances -
In Python in particular
r'\Google\Chrome\User Data' in profile_path.get() == True
is equivalent tor'\Google\Chrome\User Data' in profile_path.get() and profile_path.get() == True
(except the.get()
is only called once); this is clearly not what you intended
To fix this, omit the == True
if r'\Google\Chrome\User Data' in profile_path.get():