我正在为我的朋友开发一个小项目,它是使用 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 windoweventl = 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 因为我也使用阿拉伯输入,但这似乎不是问题,因为我尝试删除它(这不是完整的代码)。
1 回答
动漫人物
TA贡献1815条经验 获得超10个赞
它应该new.write(f'''Day of the week: {clicked.get()}''')
代替new.write(f'''Day of the week: {clicked}''')
. StringVar()
单击是您必须使用该方法访问它的值的唯一方法get()
。
希望这有帮助,如果有任何错误请告诉我。
干杯
添加回答
举报
0/150
提交
取消