Text editor python of small projects

After the entrance so you just think of this tutorial series, I am sorry, now to continue the tutorial.
In this section, previously learned knowledge to complete a gadget - a text editor!

tkinter

Before implementing a text editor, the first to understand this at tkinter python library.

tkinter GUI library used to implement the program ( the Graphical the User Interface graphical user interface), we realize there is a text editor, graphics, similar to Notepad in windows system.

Of course, not only to write GUI program tkinter library, as well as more powerful pyqt, wxpython, etc., but for this section to achieve editor, a simple tkinter library had enough, and windows installed python usually comes this library is not necessary to install.

Achieve a text editor

The first step - Interface

First of all, we first write the editor graphical interface.

what? Tkinter library did not say how it started with the?@_@

Do not panic! Very simple, look at the following code, I believe very clear Notes:

import tkinter
# 导入tkinter库


win = tkinter.Tk()  # 创建窗口
win.title('文本编辑器')  # 设置标题

entry_file = tkinter.Entry(win)  # 创建一个文本输入框,用来设置文件路径
entry_file.pack()  # 放置该输入框

def do_open():
    # 打开文件
    pass
def do_save():
    # 保存文件
    pass

btn_open = tkinter.Button(win, text='打开', command=do_open)  # 创建按钮用于打开文件
btn_save = tkinter.Button(win, text='保存', command=do_save)  # 创建按钮用于保存文件

# 放置按钮
btn_open.pack()
btn_save.pack()


# 创建多行文本框,用于编辑文件
text = tkinter.Text(win)
text.pack() 

win.mainloop()  # 进入消息循环

Use tkinter create GUI program, generally have the following framework:

win = tkinter.Tk()  # 创建窗口

# 界面的布局,如添加按钮、文本框等等

win.mainloop()  # 进入消息循环

Now you do not have to understand this framework, we did as write just fine.

Now for some places where container do explain, generally create controls (buttons, text boxes and so called controls) the first parameter is located, our program in the controls in the main window, which is the winobject.

We first create a text input box to set the path to the file operation.

Create a button using the keyword parameter textto set the label (text displayed) button, use keyword arguments commandto set things done click the button, we were created two buttons, one to open the file, save the file to a .

Finally, create a multi-line text box for editing the file.

Running the program, the window is substantially as follows:
Text editor interface

Everyone's interface style may be a bit different, this environment varies depending on the operating system
(I am lazy, because the focus of this section is not a graphical interface, so there is no interface layout to plan, only to be left unattended control directly, if interested, you can Their search tkinter specific use, a good-looking than the picture much interface ^_^)

The second step - to open the file

Now we come to realize this function to open the file, remember the one in front of such a code?

btn_open = tkinter.Button(win, text='打开', command=do_open)  # 创建按钮用于打开文件

We have to create the button tkinter.Button function ( in fact, this is not a function ), passing a parameter command=do_open, this parameter is used to set function is automatically invoked when the button is clicked, so our focus is to achieve this is called automatically when clicked do_open function.

Open the process file is kind of how it?

  1. First of all, we need to know the path to the file, starting with a text input box enter the path to the file
  2. Click the Open button
  3. Click the Open button to open the file behind the scenes, read the contents of the file, the file contents display d Austrian multi-line text box

In fact, our do_open function is above the third step, open the file => Read Data => Display data

Specific code as follows:

def do_open():
    # 打开文件
    file_path = entry_file.get()  # 获取文本框的内容
    with open(file_path) as fr:
        # 打开文件
        content = fr.read()  #一次性读取文件内容,对大文件不宜使用
        text.delete(0.0, tkinter.END)  # 清空文本框内容
        text.insert(tkinter.END, content)  # 在光标后插入内容

We firstly file_path = entry_file.get()acquires the contents of the text input box as a path to a file, the file is opened with a code block, read the file data, the data is inserted into the multi-line text box displays.

Before inserting the data, first use the text.delete(0.0, tkinter.END)empty text box, there is not much to explain. Specific usage can look yourself.

Examples are as follows (here I opened the python source code file itself):
open a file

The third step - Save the file

Achieve similar function to save the file to open the file, click the Save button: get multi-line text box contents => Open File => Write file

Man of few words said on the code!

def do_save():
    # 保存文件
    content = text.get(0.0, tkinter.END)  # 获取文本框内容
    file_path = entry_file.get()  # 文件路径
    with open(file_path, 'w') as fw:
        fw.write(content)  # 写入数据到文件中

gif dynamic presentation:
save document

First, I entered in a multi-line text box abcdefghijk, enter the text box to enter the file path 1.txt, and click Save.

After the contents of the text area are deleted, then open the 1.txt file, you can see just entered abcdefghijkcontent, browse the catalog can also be found, the current directory is indeed more than a 1.txt file, you can use Notepad to view this , save the file really successful!

The complete code

Now posted the complete code:

import tkinter
# 导入tkinter库


win = tkinter.Tk()  # 创建窗口
win.title('文本编辑器')  # 设置标题

entry_file = tkinter.Entry(win)  # 创建一个文本输入框
entry_file.pack()  # 放置该输入框

def do_open():
    # 打开文件
    file_path = entry_file.get()  # 获取文本框的内容
    with open(file_path) as fr:
        # 打开文件
        content = fr.read()  #一次性读取文件内容,对大文件不宜使用
        text.delete(0.0, tkinter.END)  # 清空文本框内容
        text.insert(tkinter.END, content)  # 在光标后插入内容

def do_save():
    # 保存文件
    content = text.get(0.0, tkinter.END)  # 获取文本框内容
    file_path = entry_file.get()  # 文件路径
    with open(file_path, 'w') as fw:
        fw.write(content)



btn_open = tkinter.Button(win, text='打开', command=do_open)  # 创建按钮用于打开文件
btn_save = tkinter.Button(win, text='保存', command=do_save)  # 创建按钮用于保存文件

# 放置按钮
btn_open.pack()
btn_save.pack()


# 创建多行文本框,用于编辑文件
text = tkinter.Text(win)
text.pack() 

win.mainloop()  # 进入消息循环

to sum up

This gadget is very simple (and ugly ( ̄_, ̄ )), but will also read and write files to the practical application of knowledge.

If you are interested in tkinter, you can search for their own learning, I believe we can make an interesting program.

Guess you like

Origin www.cnblogs.com/featherl/p/11075038.html