程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementation of multi line input text in Python GUI

編輯:Python

Catalog

Text Properties of wrap

Set the vertical scroll bar

Set the horizontal scroll bar

Get text content

Text Properties of wrapfrom tkinter import *root = Tk()root.geometry('200x300')te = Text(root,height=20,width=15)# Place multiple lines of input text in (pack) Window te.pack()# Continuous running program root.mainloop()

The height of this text box is 20, Width is 15, Unit as character . You can see that when the content of each line does not exceed 15 character , Keep typing and you will find , When the input exceeds 20 That's ok , The text box will automatically add a new line for entering new content .

in other words , In the absence of a clear statement , When entering multiple lines of text , The content is unlimited , But there is a limit to how many characters can be entered per line .

Of course, the contents that can be entered in each line can also be unlimited , This is to say wrap attribute .

wrap and Label and Button Of wraplength Properties are different .wraplength To fully display the text content of a label or button , Multi line display . and Text The contents of can be completely displayed ,wrap Take different values . The display mode is different .

wrap There can be three values :NONE、WORD、CHAR.

# The other parts are the same as above , Just added a wrap attribute te = Text(root,height=20,width=15,wrap=NONE)

Height and width are still 20、15, Just properties wrap The value of is NONE. In this case , Without manual line wrapping , Each line can be entered continuously . If you put the contents of the file into the text box , such as text、word file , The contents of each line in the multi line text box are consistent with those in the file .

#wrap=CHARte = Text(root,height=20,width=15,wrap=CHAR)

#wrap=WORDte = Text(root,height=20,width=15,wrap=WORD)

Above is wrap The value is WORD and CHAR The difference between . You can see that the value is WORD when , Text boxes guarantee character integrity , And the value is CHAR Not when . both , Each line will be limited to 15 Characters , It's just that the way you wrap lines is different .

When there is more content in the text box , You can use the scroll bar to browse the content . There are vertical scrollbars 、 Horizontal scroll bar .

Set the vertical scroll bar

Desired effect : When we slide the vertical scroll bar , The content of the text changes from top to bottom .

import tkinter as tkroot=tk.Tk()root.geometry('300x240')#f=tk.Frame(root)# Create a scroll bar object , The default is the vertical scroll bar ,orient=tk.VERTICALs1 = tk.Scrollbar(root) # The scroll bar is to be placed on the window , Its parameters are window objects # Create a multiline input text object b1 = tk.Text(root,width=20,height=5, yscrollcommand=s1.set)# Get the contents of the text file with open(r'C:\Users\Administrator\Desktop\ Du Fu .txt') as a: data = a.read()# Put the contents of the text file into a text box b1.insert(tk.END,data)s1.pack(side=tk.RIGHT,fill=tk.Y)s1.config(command=b1.yview)b1.pack()#f.pack()root.mainloop()

Effect diagram

After creating the vertical scroll bar object and the multiline input text object , We need to pay attention to :

1.yscrollcommand=s1.set, This slides the contents of the text box , The scroll bar also slides

2.s1.config(command=b1.yview), Slide the scroll bar like this , The contents of the text box will also slide .

3.s1.pack(side=tk.RIGHT,fill=tk.Y), In this way, the scroll bar can be filled with y Axis direction , Not just a small part .

Set the horizontal scroll bar import tkinter as tkroot=tk.Tk()root.geometry('300x240')#f=tk.Frame(root)s1 = tk.Scrollbar(root,orient=tk.HORIZONTAL)b1 = tk.Text(root,width=20,height=5,wrap=tk.NONE, xscrollcommand=s1.set,cursor='circle', selectbackground='yellow', selectforeground='blue')#text Initialization of content with open(r'C:\Users\Administrator\Desktop\ Du Fu .txt') as a: data = a.read()# Put it in Dufu's 《 The hut is broken by the autumn wind 》b1.insert(tk.END,data)b1.pack()# Get the content in the text #print(b1.get(1.4,3.5),type(b1.get(1.4,3.5)))s1.pack(side=tk.BOTTOM,fill=tk.X)s1.config(command=b1.xview)#f.pack()root.mainloop()

Effect diagram

There is a problem with setting the horizontal scroll bar , That is, the scroll bar does not display . This is because , By default , There is a limit to the number of characters per line , This eliminates the need for horizontal scrollbars . If you want to remove the restriction , Or make the text content put in wrap in its original way , Need to add wrap=NONE.

Get text content

We know Label、Button There are text attribute , Unlike the two ,Entry、Text Out-of-service text Property to fill in the text content ,Entry It uses textvariable attribute ,Text Use insert Insert text content ,Entry and Text The text content of is dynamic , You can enter changed .

Entry and Text Text content can be entered , It's just Text You can enter multiple lines . therefore Entry No, height attribute , and Text Yes . stay Entry and Text After entering the text content , It can be used get() Get the text content in string form .

import tkinter as tkroot=tk.Tk()root.geometry('300x240')#f=tk.Frame(root)s1 = tk.Scrollbar(root,orient=tk.HORIZONTAL)b1 = tk.Text(root,width=20,height=5,wrap=tk.NONE, xscrollcommand=s1.set,cursor='circle', selectbackground='yellow', selectforeground='blue')#text Initialization of content with open(r'C:\Users\Administrator\Desktop\ Du Fu .txt') as a: data = a.read()# Put it in Dufu's 《 The hut is broken by the autumn wind 》b1.insert(tk.END,data)b1.pack()# Get the content in the text #print(b1.get(1.4,3.5),type(b1.get(1.4,3.5)))s1.pack(side=tk.BOTTOM,fill=tk.X)s1.config(command=b1.xview)#f.pack()#button Callback function for def pri1(): print(b1.get(1.0, 5.0), type(b1.get(1.4, 3.5)))# Because the text content can change at any time ,button Get the latest text content button = tk.Button(root,text='button',command=pri1)button.pack()root.mainloop()

Click on Button Button , Will get the text content

What we are intercepting here is a paragraph . Every click of a button , Get the text content once . So create Button Button to get the current content of the text .

The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved