为什么我的python tkinter代码不起作用?

时间:2019-06-22 19:36:33

标签: python-3.x tkinter

  • 列表项

from tkinter import *类菜单栏:def init (自身,父):font_specs =(“ ubuntu”,14)菜单栏= Menu(父.master,font = font_specs)parent.master。 config(menu = menubar)file_dropdown =菜单(menubar,font = font_specs,tearoff = 0)file_dropdown.add_command(label =“ New File”,加速器=“ Ctrl + N”,command = parent.new_file)file_dropdown.add_command(label =“打开文件”,加速器=“ Ctrl + O”,命令= parent.open_file)file_dropdown.add_command(label =“保存”,加速器=“ Ctrl + S”,command = parent.save)file_dropdown.add_command(label = “另存为”,加速器=“ Ctrl + Shift + S”,命令= parent.save_as)file_dropdown.add_separator()file_dropdown.add_command(label =“ Exit”,command = parent.master.destroy)about_dropdown = Menu(菜单, font = font_specs,tearoff = 0)about_dropdown.add_command(label =“ Release Notes”,command = self.show_release_notes)about_dropdown.add_separator()about_dropdown.add_command(label =“ About”,command = self.show_about_message)menubar.add_cascade( label =“ File”,menu = file_dropdown)menubar.add_cascade(label =“ About”,menu = about_dropdown)def show_about_message(自我):box_title =“关于PyText” box_message =“一个简单的Python文本编辑器” messagebox.showinfo(box_title,box_message)def show_release_notes(自我) :box_title =“发行说明” box_message =“ Version 0.1-Gutenberg” messagebox.showinfo(box_title,box_message)类状态栏:def init (自己,父级):font_specs =(“ ubuntu”,12) self.status = StringVar()self.status.set(“ PyText-0.1 Gutenberg”)标签= Label(parent.textarea,textvariable = self.status,fg =“ black”,bg =“ lightgrey”,anchor ='sw ',font = font_specs)label.pack(side = BOTTOM,fill = BOTH)def update_status(self,* args):如果isinstance(args [0],bool):self.status.set(“您的文件已保存!“)其他:self.status.set(” PyText-0.1 Gutenberg“)类PyText:def init (自我,主人):master.title(”无标题-PyText“)master.geometry( “ 1200x700”)font_specs =(“ ubuntu”,18)self.master =主self.filename = N一个self.textarea =文本(master,font = font_specs)self.scroll =滚动条(master,command = self.textarea.yview)self.textarea.configure(yscrollcommand = self.scroll.set)self.textarea.pack(side =左,填充=两个,展开=真)self.scroll.pack(side = RIGHT,fill = Y)self.menubar = Menubar(self)self.statusbar = Statusbar(self)self.bind_shortcuts()def set_window_title(self) ,name = None):如果名称:self.master.title(name +“-PyText”)否则:self.master.title(“ Untitled-PyText”)def new_file(self,* args):self.textarea.delete (1.0,END)self.filename = None self.set_window_title()def open_file(self, args):self.filename = filedialog.askopenfilename(defaultextension =“。txt”,filetypes = [(“ All Files”, “ ”),(“文本文件”,“ .txt”),(“ Python脚本”,“ .py”),(“ Markdown文档”,“ .md”),(“ JavaScript文件”,“ .js”),(“ HTML文档”,“ .html”),(“ CSS文档”,“ *。css” “)])如果self.filename:self.textarea.delete(1.0,END)的open(self.filename,” r“)为f:self.te xtarea.insert(1.0,f.read())self.set_window_title(self.filename)def save(self,* args):if self.filename:try:textarea_content = self.textarea.get(1.0,END)打开(self.filename,“ w”)as f:f.write(textarea_content)self.statusbar.update_status(True)例外,例如e:print(e)else:self.save_as()def save_as(self, args):尝试:new_file = filedialog.asksaveasfilename(initialfile =“ Untitled.txt”,defaultextension =“。txt”,filetypes = [(“ All Files”,“ ”),(“”文本文件”,“ .txt”),(“ Python脚本”,“ .py”),(“ Markdown文档”,“ .md”),(“ JavaScript文件”, “ .js”),(“ HTML文档”,“ .html”),(“ CSS文档”,“ * .css”)])textarea_content = self.textarea.get(1.0,END ),以open(new_file,“ w”)作为f:f.write(textarea_content)self.filename = new_file self.set_window_title(self.filename)self.statusbar.update_status(True)例外,例如e:print(e)def bind_shortcuts(self):self.textarea.bind('',self.new_file)self.textarea.bind('',self.open_f ile)self.textarea.bind('',self.save)self.textarea.bind('',self.save_as)self.textarea.bind('',self.statusbar.update_status)如果名称 ==“ 主要”:master = Tk()pt = PyText(master)master.mainloop()

0 个答案:

没有答案