[問題] 請問寫入檔案(新手)

看板Python作者 (趴啦嘉)時間12年前 (2014/01/03 11:47), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/1
Hi,大家好 想請教寫檔的問題 我目前在用python寫一個記事本出來 可是目前寫到存檔功能卻出不來 以下是我目前的程式碼(有很多功能還在努力) import tkinter from tkinter import * from scrolledtext import * import filedialog import messagebox root = Tk(className=" 記事本 ") textPad = ScrolledText(root, width=100, height=80) # create a menu & define functions for each menu item def open_command(): file = filedialog.askopenfile(parent=root,mode='rb',title='Select a file') if file != None: contents = file.read() textPad.insert('1.0',contents) file.close() def save_command(self): file = filedialog.asksaveasfile(mode='w') if file != None: # slice off the last character from get, as an extra return is added data = self.textPad.get('1.0', END+'-1c') file = open('text.txt','w') file.write(data) file.close() def exit_command(): if messagebox.askokcancel("Quit", "Do you really want to quit?"): root.destroy() def about_command(): label = messagebox.showinfo("About", "Just Another TextPad \n Copyright \n No rights left to reserve") def dummy(): print ("I am a Dummy Command, I will be removed in the next step") menu = Menu(root) root.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=dummy) filemenu.add_command(label="Open...", command=open_command) filemenu.add_command(label="Save", command=save_command) filemenu.add_separator() filemenu.add_command(label="Exit", command=exit_command) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=about_command) # textPad.pack() root.mainloop() 我用的版本是python3.3.3 他 會跑出TypeError:save_command() -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 49.218.103.119

01/03 11:51, , 1F
剛剛發文到一半就送出了…TypeError:save_command() missi
01/03 11:51, 1F

01/03 11:52, , 2F
required positional argument:'self'
01/03 11:52, 2F
文章代碼(AID): #1InZBAqr (Python)
文章代碼(AID): #1InZBAqr (Python)