我正在创建一个程序来通过discord传输用户输入,为此你需要2个discord机器人,所以我为令牌创建了一个设置文件,这个设置文件工作正常,它打开一个tk窗口,在其中要求数据,但随后它会给我权限错误:
PermissionError: [Errno 13] Permission denied: 'vars.py'
这是我的代码,我过去在写入文件时感到困惑(在replit中)我没有遇到任何权限错误。
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
def write_to_file(file_path, text):
with open(file_path, 'w') as file:
file.write(text)
def submit():
vc_id = vc_id_entry.get()
sender_token = sender_token_entry.get()
receiver_token = receiver_token_entry.get()
write_to_file("vars.py", f"vc_id = {vc_id}\ns_token = \"{sender_token}\"\nr_token = \"{receiver_token}\"\nsetup = True")
messagebox.showinfo("Setup Done", "You may now close the window")
# Create main window
root = tk.Tk()
root.title("Setup")
# Create labels and entry boxes
vc_id_label = ttk.Label(root, text="VC ID:")
vc_id_label.grid(row=0, column=0, padx=10, pady=10, sticky=tk.W)
vc_id_entry = ttk.Entry(root, width=40)
vc_id_entry.grid(row=0, column=1, padx=10, pady=10)
sender_token_label = ttk.Label(root, text="Sender Token:")
sender_token_label.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W)
sender_token_entry = ttk.Entry(root, width=40)
sender_token_entry.grid(row=1, column=1, padx=10, pady=10)
receiver_token_label = ttk.Label(root, text="Receiver Token:")
receiver_token_label.grid(row=2, column=0, padx=10, pady=10, sticky=tk.W)
receiver_token_entry = ttk.Entry(root, width=40)
receiver_token_entry.grid(row=2, column=1, padx=10, pady=10)
# Submit button
submit_button = ttk.Button(root, text="Submit", command=submit)
submit_button.grid(row=3, columnspan=2, pady=10)
# Start GUI
root.mainloop()
我尝试使用 chatgpt 来修复它,但它只是给了我一大堆废话。我期待它运行并将其要求的变量填充到 vars.py 文件中,该文件将被主文件访问以运行不和谐机器人。
你用 sudo 运行它吗?确保使用必要的权限运行脚本。
示例:
sudo python3 vars.py