下拉菜单给出 txt 文件中的 PY_VAR0

我正在为我的朋友开发一个小项目,它是使用 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 因为我也使用阿拉伯输入,但这似乎不是问题,因为我尝试删除它(这不是完整的代码)。


MYYA
浏览 307回答 1
1回答

动漫人物

它应该new.write(f'''Day of the week: {clicked.get()}''')代替new.write(f'''Day of the week: {clicked}'''). StringVar()单击是您必须使用该方法访问它的值的唯一方法get()。希望这有帮助,如果有任何错误请告诉我。干杯
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python