2017-04-16 125 views
0

如何修改我用Tkinter打開的文件(添加或刪除)文件? 例如,當我用notebad打開某個文件時,我可以輕鬆修改文本。 我不知道如何在tkinter中做到這一點。 有我的代碼:用tkinter修改文本文件python3

from tkinter import * 
from tkinter import filedialog 
import re 

class Window(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master = master 

     self.master.title("JoNotepad") 
     self.pack(fill=BOTH, expand=1) 

     menu = Menu(top) 
     top.config(menu=menu) 
     self.file_menu = Menu(menu) 
     menu.add_cascade(label="File", menu=self.file_menu) 
     self.file_menu.add_command(label="New") 
     self.file_menu.add_command(label="Open",  command=self.open_file_function) 
     self.file_menu.add_command(label="Save") 
     self.file_menu.add_separator() 
     self.file_menu.add_command(label="Exit") 

     self.listNodes = Listbox(top, height=200, width=200) 
     self.listNodes.pack(side=LEFT, fill=Y, expand=True) 

     self.scrollbar = Scrollbar(top, orient="vertical") 
     self.scrollbar.config(command=self.listNodes.yview) 
     self.scrollbar.pack(side=RIGHT, fill=Y, expand=True) 

     self.listNodes.config(yscrollcommand=self.scrollbar.set) 


    def open_file_function(self): 

     self.file_save = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("txt files", "*.txt"), ("All files", "*.*"))) 
     with open(self.file_save) as file: 
      for i in file: 
       self.listNodes.insert(END, i) 


top = Tk() 
top.geometry("1000x1000") 
ap = Window(top) 

top.mainloop() 

回答

0

,您應該使用tkinter Text widget代替ListboxText小部件允許您執行想要執行的操作(包括添加文本,刪除文本,選擇文本等)。

這是您的代碼,使用Text小部件。

from tkinter import * 
from tkinter import filedialog 
import re 

class Window(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master = master 

     self.master.title("JoNotepad") 
     self.pack(fill=BOTH, expand=1) 

     menu = Menu(top) 
     top.config(menu=menu) 
     self.file_menu = Menu(menu) 
     menu.add_cascade(label="File", menu=self.file_menu) 
     self.file_menu.add_command(label="New") 
     self.file_menu.add_command(label="Open",  command=self.open_file_function) 
     self.file_menu.add_command(label="Save") 
     self.file_menu.add_separator() 
     self.file_menu.add_command(label="Exit") 

     self.text = Text(top, height=200, width=200) #Use Text widget insted of Listbox 
     self.text.pack(side=LEFT, fill=Y, expand=True) 

     self.scrollbar = Scrollbar(top, orient="vertical") 
     self.scrollbar.config(command=self.text.yview) 
     self.scrollbar.pack(side=RIGHT, fill=Y, expand=True) 

     # change all occurances of self.listNodes to self.text 
     self.text.config(yscrollcommand=self.scrollbar.set) 


    def open_file_function(self): 

     self.file_save = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("txt files", "*.txt"), ("All files", "*.*"))) 
     with open(self.file_save) as file: 
      for i in file: 
       self.text.insert(END, i) 


top = Tk() 
top.geometry("1000x1000") 
ap = Window(top) 

top.mainloop() 
+0

謝謝。有效。 –