我正在為我的朋友開發一個小項目,它是使用 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
提交
取消
