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

Python Tkinter - Chapter 10 text control properties

編輯:Python

The first 10 Chapter Text control (Text)
Text control (Text) A control used to display multiple lines of formatted text . Text controls are powerful , Very flexible , It can do a lot of things .. In addition to displaying multiple lines of text , You can also edit text , display picture , Even web pages .
You can put words 、 identification (marks)、 Pictures and embedded windows are placed in text controls . Different formats can be displayed in different areas . If you associate the callback function with events in different regions , You can also make different responses to different areas .
By default , Text controls are editable . You can use the mouse or keyboard to edit . If you just want to display text or pictures , You can disable the editing function of the text control , Just set up state=tk.DISABLED That's all right. .
10.1 attribute

Property parameters describe autoseparators The spacing between words . The default value is 1background
bg Set the background color , Such as bg=‘green’borderwidth
bd The border width of the text control . The default is 1-2 Pixel .cursor The cursor of the text control . The default is character insertion cursor ( It's usually a “I-beam” Cursor of type )exportselection Whether to allow copying contents to the clipboard foregroundfg Set foreground ( Text ) Color font Set font type and size height The height of the text control . The default is 24 That's ok .highlightbackground Define the highlighted background color when the text control does not get the input focus state . Is the bright edge of the text control .highlightcolor And highlightbackground Properties are similar to . It's just the border color when the text control gets the input focus .highlightthickness The width of the border when the text control gets the input focus . commonly 1-2 Pixel .insertbackground Set the color of the text control insertion cursor insertborderwidth Insert the border width of the cursor . If it is a non 0 The numerical , The cursor will use RAISED The border of the effect .insertofftime
insertontime These two properties control the blinking effect of the insertion cursor . Is the time when the insertion cursor appears and disappears . In milliseconds .insertwidth Set the width of the insertion cursor .maxundo Maximum Undo The number of times . The default is 0.padx The inside margin of the horizontal border pady The inside margin of the vertical border relief Specify the border of the text control 3D effect , The default is flat, Parameters that can be set ;flat、groove、raised、ridge、solid、sunkenselectbackground Set the background color of the selected text selectborderwidth Set the boundary width of the selected area .selectforeground Set the color of the selected text setgridboolean type . by True when , You can maximize the window , And display the whole Text Control spacing1 Uplink spacing . If there is a line break , It only works on the first line spacing2 Line spacing between lines spacing3 Down spacing . If there is a line break , Only on the last line .state Define the state of the text control . There are two states :NORMAL and DISABLEDtabs Define the button Tab Key movement distance .takefocus Define whether you can use Tab Key to move the input focus to the control .undo Turn on undo/redo function .width Define the width of the text control , The unit is the number of characters .wrap Defines how to wrap the contents of a text control xscrollcommand
yscrollcommand Associate a scroll bar with a text control , Handle scroll bar actions . Scroll bars correspond to horizontal or vertical scroll bars respectively .10.1.1 autoseparators see undo/redo10.1.2 background(bg) Set the background color of the text control .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,bg='green')
b1.pack()
root.mainloop()

result :

10.1.3 cursor
Define the cursor type of the text control . The default is ’ Insert cursor ’. For details, see 3.3.6 section

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,cursor='mouse')
b1.pack()
root.mainloop()

result :

10.1.4 exportselection
Is a Boolean value , It has no effect in text control .
10.1.5 foreground(fg)
Define the color of the input text . The default is black .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,fg='red')
b1.pack()
root.mainloop()

result :

10.1.6 font
Define the font size . Text control can set multiple fonts to display at the same time . For specific methods, see tag.
10.1.7 Height and width
Define the height and width of the text control . Height in behavioral units , The width is in characters . The measurement of height and width is also related to the font size .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=10)
b1.pack()
root.mainloop()

result :

10.1.8 highlightbackground、highlightcolor and highlightthickness
The function of these three parameters is to add a frame to the text control . When you don't get input focus , The color of the outer frame is determined by highlightbackground Set up , The color of the outline when the focus is input by highlightcolor Set up . The width of the outer frame is determined by highlightthickness Set up .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=10,
highlightthickness=5,highlightcolor='red',
highlightbackground='yellow')
b1.pack()
b2=tk.Entry(root)
b2.pack()
root.mainloop()

result :


10.1.9 insertbackground and insertborderwidth
The background color and width of the insertion cursor . See input control (Entry) The instructions in .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,insertbackground ='red')
b1.pack()
root.mainloop()

result :

explain : The insertion cursor has turned red .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,
insertbackground ='red',
insertborderwidth=-20)
b1.pack()
root.mainloop()

result :

explain :insertborderwidth Is to set the border of the insertion cursor . But positive values have only one effect , Instead, a negative value can be set to insert the border of the cursor according to the given value .
10.1.10 insertofftime and insertontime
These two parameters set the time when the cursor flashes .insertofftime Is the time when the cursor is not displayed ,insertontime Is the time displayed by the cursor . In this way, the cursor flickering effect can be realized . In milliseconds .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,insertofftime=200,insertontime=1000)
b1.pack()
root.mainloop()

10.1.11 insertwidth
Define the width of the insertion cursor . Unit is pixel .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,insertwidth=20)
b1.pack()
root.mainloop()

result :

10.1.12 maxundo
See 10.1.23 undo/redo chapter .
10.1.13 padx and pady
Set the horizontal and vertical inside margins of the text control . Please refer to the description in the previous chapter .
10.1.14 relief
Of the border of the text control 3D effect . For details, see 3.3.5 section .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,bd=5,relief='groove')
b1.pack()
root.mainloop()

result :

10.1.15 selectbackground
The background color of the selected text . The default is blue .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,selectbackground='red')
b1.pack()
root.mainloop()

result :


10.1.16 selectborderwidth
The border width of the selected area .
10.1.17 selectforeground
The font color of the selected text . The default is white .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,height=5,width=30,selectforeground='yellow')
b1.pack()
root.mainloop()

result :

10.1.18 setgrid
It's a boolean Parameters . If set to True, Maximizes the window , This will show the complete Text Control .
10.1.19 spacing1,spacing2 and spacing3
spacing1: Additional uplink spacing . If there is a line break , Increase spacing only on the first line
spacing2; Line spacing of broken lines
spacing3: Additional downlink spacing . If there is a line break , Show only on the last line .

# Code 1:
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root)
b1.pack()
root.mainloop()
# Code 2:
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,width=25,spacing1=20,
spacing2=30,spacing3=50)
b1.pack()
root.mainloop()

result :


explain :
The distance between the first row and the top of Figure 2 , Is the uplink spacing , Yes spacing1 Set up
Line break in Figure 2 , from spacing2 Set up . What is set in the program is 30, You can see that there is a clear gap .
The largest spacing in Figure 2 is the downlink spacing ,spacing3=50.
Compare it with the figure , Because figure 1 does not have any spacing settings .
10.1.20 state
The text control only has 2 States :NORMAL and DISABLED.NORMAL Status can be edited 、 Input text and other functions . and DISABLED You can't do anything .

10.1.21 tabs
tabs Is to set the text control tab The location of . The default value is 8 Characters , That is, every time you press Tab key , The insertion cursor moves 8 Characters . If you have entered 2 Characters , So just move 6 Characters . That is, every time you move 8 Multiples of characters .

tabs Property to set the distance of each move , See the description below for specific units :
No parameter : Represents moving in pixel units
c= centimeter
i= Inch
M= mm
P= Printer point , namely 1/70 Inch

explain : default Tabs by 8 Characters , Not affected by font size

Tab The movement of can be nonlinear , That is to say, the distance of each movement can be different , Each defined moving distance can be declared as a tuple , Assign a value to tabs attribute , such as :

10.1.22 takefocus
Set whether you can pass Tab Get input focus . The default is yes . If takefocus Set to False, Can't use Tab Key to get input focus , Only use the mouse to operate .

10.1.23 undo/redo
Text control supports undo/redo function . Default is not supported , It needs to be set by undo=True To open undo/redo function . This function is realized by recording every modification . Every time you insert or delete , Are recorded on a stack . If delete , Record the deleted text ; If it's insertion , Record the inserted text . At the same time , There are also locations where records are deleted or inserted .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,undo=True)
b1.pack()
root.mainloop()

Undo/redo It is judged by the movement of the cursor . If the cursor is moved sequentially , No new tags will be inserted . For example, we entered “123and456” If we move the cursor back to ”and” It's about , And delete ”and”, The built-in algorithm inserts a tag , It's the same as what we input ”123and456” Compartmentalize . If this time , call undo function , Will recover first ’and’, And then delete it “123and456”

These tags are recorded by default . It can actually be done by autoseparators Property to set . By default autoseparators=True. If you put autoseparators=False, Each modification will not be recorded .Undo All entries will be deleted ,redo Restore everything . Simple and crude .
Another attribute is maxundo, Record the largest undo Times . The default value is 0, That is, record all modification actions . If you set maxundo=5, Just record 5 Modification behavior .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,undo=True,maxundo=5)
b1.pack()
root.mainloop()

result :


explain : Set up maxundo=5 after , The system only records 5 Time modification , Can only undo5 Time .
10.1.24 wrap
wrap Property to set how to wrap the contents of the text control . There are two ways to wrap lines :
(1) Character separation
When a line break occurs , This method can separate at any character .
(2) Word separation
When a line break occurs , This method will separate by word .

# Code 1:
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,width=15,wrap=tk.CHAR)
b1.pack()
root.mainloop()
# Code 2:
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,width=15,wrap=tk.WORD)
b1.pack()
root.mainloop()

result :


The line break display can also be turned off . At this time, all the characters are on one line , Unless you enter enter .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Text(root,width=15,wrap=tk.NONE)
b1.pack()
root.mainloop()

10.1.25 xscrollcommand
Add a horizontal scroll bar to the text control .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
f=tk.Frame(root)
s1 = tk.Scrollbar(f,orient=tk.HORIZONTAL)
b1 = tk.Text(f,width=20,height=5,
xscrollcommand=s1.set,wrap=tk.NONE)
b1.pack()
s1.pack(side=tk.BOTTOM,fill=tk.X)
s1.config(command=b1.xview)
f.pack()
root.mainloop()

result :

10.1.26 yscrollcommand
Add a vertical scroll bar to the text control .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
f=tk.Frame(root)
s1 = tk.Scrollbar(f,orient=tk.VERTICAL)
b1 = tk.Text(f,width=20,height=5,
yscrollcommand=s1.set,wrap=tk.NONE)
s1.pack(side=tk.RIGHT,fill=tk.Y)
s1.config(command=b1.yview)
b1.pack()
f.pack()
root.mainloop()

result :

You can also add horizontal and vertical scroll bars to text controls .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
f=tk.Frame(root)
s1 = tk.Scrollbar(f,orient=tk.VERTICAL)
s2 = tk.Scrollbar(f,orient=tk.HORIZONTAL)
b1 = tk.Text(f,width=20,height=5,wrap=tk.NONE,
yscrollcommand=s1.set,
xscrollcommand=s2.set)
s1.pack(side=tk.RIGHT,fill=tk.Y)
s1.config(command=b1.yview)
s2.pack(side=tk.BOTTOM,fill=tk.X)
s2.config(command=b1.xview)
b1.pack(fill=tk.BOTH)
f.pack()
root.mainloop()

result :


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