python - ttk.Entry.select_range() 适用于 Button,但不适用于 python/tkinter 中的 ttk.Button?

标签 python button tkinter

有人可以解释一下为什么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()

最佳答案

这个答案来自Google Group说,

However, on Windows (only) the selection will only become visible when the entry gets the focus.

还有此页面关于ttk button说,

By default, a ttk.Button will be included in focus traversal ... To remove the widget from focus traversal, use takefocus=False

所以你需要为ttk.Button添加takefocus选项。

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

关于python - ttk.Entry.select_range() 适用于 Button,但不适用于 python/tkinter 中的 ttk.Button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25039390/

相关文章:

python - min() arg 是一个空序列

python - 消息读取脚本显示机器人自己的消息并永远循环

javascript - Jquery 中的 Css 之后和之前

Python tkinter 输入提示

python - Tkinter 无法正确关闭并启动新文件

Python连接列表中的相邻字符串元素

python - WTForms 在 Heroku 上引发 "TypeError: ' unicode' 没有缓冲区接口(interface)

android - 如何在android中为按钮添加边框

当自定义按钮更改文本时,Java 布局不会更新组件大小

python tkinter Canvas 'transparent'