所以我试图编写一种方法来保存用户选择的图片并将图片添加到父类“activity”中。这是我尝试过的:
# Creating object from filedialog
picture = filedialog.asksaveasfile(mode='r', title='Select activity picture', defaultextension=".jpg")
# Check if cancelled
if picture is None:
return
else:
folder_dir = 'main/data/activity_pics'
pic_name = 'rocket_league' #change to desc_to_img_name() later
path = os.path.join(folder_dir, f'{pic_name}')
# Making the folder if it does not exist yet
if not os.path.exists(folder_dir):
os.makedirs(folder_dir)
# Creating a location object
f = open(path, "a")
# Writing picture on location object
f.write(picture)
# Closing
f.close()
# Check if successful
if os.path.exists(path):
print(f'File saved as {pic_name} in directory {folder_dir}')
self.picture_path = path
后来,我使用 调用该方法rocketleague.add_picture(),其中我将 Rocketleague 定义为一个Activity类。
我尝试了在网上找到的几种不同的解决方案,但似乎都不起作用。目前,该脚本出现错误:
C:\Users\timda\PycharmProjects\group-31\venv\Scripts\python.exe C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.py
Traceback (most recent call last):
File "C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.py", line 160, in <module>
rocketleague.add_picture()
File "C:/Users/timda/PycharmProjects/group-31/project/activities/random_activity_generator.py", line 46, in add_picture
f.write(picture)
TypeError: write() argument must be str, not _io.TextIOWrapper
所以我猜我的 open.write() 除了字符串之外不适合任何东西。我不确定我当前的方法是否有效。
执行此操作最方便的方法是什么?
呼啦一阵风
相关分类