简体   繁体   English

ttk.Entry.select_range()可与Button一起使用,但不能在python / tkinter中使用ttk.Button吗?

[英]ttk.Entry.select_range() works with Button, but not ttk.Button in python / tkinter?

Can some one please explain why entry.select_range() works with a Button, but not a ttk.Button? 有人可以解释一下为什么entry.select_range()与Button一起使用,而不与ttk.Button一起使用吗?

from tkinter import *
from tkinter import ttk

root = Tk()

entry = ttk.Entry(root)
entry.pack()

#This works
button = Button(root, text="Select your text", command=lambda:
                    entry.select_range(0, END))

#but this doesn't
##button = ttk.Button(root, text="Select your text", command=lambda:
##                    entry.select_range(0, END))

button.pack()

root.mainloop()

This answer from Google Group says, Google Group的这个回答说:

However, on Windows (only) the selection will only become visible when the entry gets the focus. 但是,(仅在Windows上 )仅当条目获得焦点时,该选择才可见。

and also this page about ttk button says, 还有关于ttk按钮的页面说,

By default, a ttk.Button will be included in focus traversal ... To remove the widget from focus traversal, use takefocus=False 默认情况下,焦点遍历中将包含一个ttk.Button ...要从焦点遍历中删除小部件,请使用takefocus = False

So you need to add takefocus option to ttk.Button . 所以,你需要takefocus选项添加到ttk.Button

button = ttk.Button(root, takefocus=False, text=...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM