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()