我正在为我的朋友开发一个小项目,它是使用 python tkinter 进行的。
代码是
def run():
filename = filename_ent.get()
if filename == '':
# to show an error that boxes are empty
messagebox.showerror(
'File exists', 'File already exists, try some other name thas is not used before')
if os.path.exists(f'{filename}.txt'):
# to show an error if the file already exists
messagebox.showerror(
'File exists', 'File already exists, try some other name not used before')
else:
# to open the file for python
new = open(f'{filename}.txt', '+w', encoding='utf-8')
# to write the name and email inside the file
new.write(f'''Day of the week: {clicked}''')
eventl.config(text=f'Done :)!') # to change the label to the name
os.startfile(f'{filename}.txt') # to open the file in a new window
eventl = Label(root, text='What is the name of the event',font=('helvatica',14))
eventl.place(x=0,y=0)
lfilename = Label(root,text="What do you want to call the file?", font = ("helvatica, 14"))
lfilename.pack()
filename_ent = Entry(root)
filename_ent.pack(pady=10,padx=30)
clicked = StringVar()
drop = OptionMenu(root, clicked, "Monday", "Tuesday")
drop.pack(pady=10,padx=10)
b = Button(root, text='Done', command=run)
b.pack(pady=(10, 0))
root.mainloop()
注意:我使用 utf-8 因为我也使用阿拉伯输入,但这似乎不是问题,因为我尝试删除它(这不是完整的代码)。
动漫人物
相关分类