在Entry插件里,scan_mark()和scan_dragto()应该是比较少见的方法,
查了很多资料也没有搞清楚,我写一下我自己的发现吧。
Entry对象是一个单行文本输入栏,这两个方法应该是对文本长度大于输入栏的情况的。
首先创建了一个Entry对象,将输入框内容提前设置为一串长字符串。
- import tkinter as tk
- window = tk.Tk()
- window.geometry("300x200")
- var = tk.StringVar()
- var.set("0123456789abcdefghijklmnopqrstuvwxyz0123456789")
- e1 = tk.Entry(window,show = None,font = ('黑体',12),bd=3,width = 15,textvariable = var)
- e1.pack()
复制代码我没有使用绑定的方法,而是用一个按钮调用这两个方法。
- def b_func():
- e1.scan_mark(0)
- e1.scan_dragto(-2)
- #创建一个按钮并放置,点击按钮调用b_func函数
- button = tk.Button(window,text = "button",font =('Arial',10),command = b_func)
- button.pack()
- window.mainloop()
复制代码这样,可以通过调整两个方法的参数看结果。
经过尝试,这两个方法可能是调整输入栏中文本的位置的,调整的距离为两个方法的参数的差,
例如, e1.scan_mark(0),e1.scan_dragto(-2)的作用是:每次按下按钮,文本向右移动两格。
试了几个其他的数也是这样。
但是当参数比较大的时候,比如4和-1,却是每次向右移动6格,可能是字体的原因?
另外,我在shell里输入help(tkinter.Entry),在文档里找到这样一段:
| Methods defined here:
……
| scan_dragto(self, x)
| Adjust the view of the canvas to 10 times the
| difference between X and Y and the coordinates given in
| scan_mark.
|
| scan_mark(self, x)
| Remember the current X, Y coordinates.
感觉不常用……
希望对你有帮助。