我開發了一個程式,它會詢問用戶Profile PathChrome 瀏覽器的路徑(可以通過chrome://version/在 Chrome 瀏覽器的地址欄中輸入來獲取),用戶必須將該路徑復制并粘貼到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
圖形界面預覽:

但是,它并沒有按預期作業,因為即使用戶提供了正確的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\\(在我的情況下是默認),然后執行其余的代碼。
我想知道上面的代碼中還缺少什么才能按預期作業?(即執行條件if profile_path.get().split("User Data\\",1)[1] != None:)
uj5u.com熱心網友回復:
一般來說,你通常不想比較,
== 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():
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/413292.html
標籤:
