用tkinter python3修改文本文件

时间:2017-04-16 08:06:36

标签: python-3.x tkinter

如何修改我用Tkinter打开的文件(添加或删除)文件? 例如,当我用notebad打开一些文件时,我可以轻松修改文本。 我无法弄清楚我怎么能在tkinter中做到这一点。 有我的代码:

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

1 个答案:

答案 0 :(得分: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()