[英]selection moves from ttk.Entry to tkinter.Text
Here's a python/tkinter program that puzzles me.这是一个让我困惑的 python/tkinter 程序。 The window displays a ttk.Entry that is readonly and a tkinter.Text that is disabled.
该窗口显示只读的 ttk.Entry 和禁用的 tkinter.Text。 It programmatically selects one character in the Entry box and never changes this selection.
它以编程方式在输入框中选择一个字符,并且永远不会更改此选择。 However the selection will change if I try to select text in the other box (the disabledText).
但是,如果我尝试在另一个框(disabledText)中选择文本,则选择会改变。 This doesn't seem right.
这似乎不对。
Python 3.5.0 and tcl/tk 8.5.18 on OS X OS X 上的 Python 3.5.0 和 tcl/tk 8.5.18
If somebody can explain this, I'd be most grateful.如果有人可以解释这一点,我将不胜感激。
import tkinter
from tkinter import ttk
class ButtonPanel(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.data = ttk.Entry(self, width=27, takefocus=False)
self.data.insert(0, "ABCDEFG")
self.data.select_range(0, 1) # select the "A"
self.data.state(["readonly"])
self.data.bind('<ButtonPress>', lambda e: 'break') # ignore mouse clicks
button = ttk.Button(self, text="Write Data", command=self.master.write)
self.data.grid(column=0, row=0, padx=10)
button.grid(column=1, row=0, padx=10)
def get(self):
return self.data.selection_get() # should always be the "A"
class App(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.bp = ButtonPanel(self)
self.display = tkinter.Text(self, width=50, height=10, wrap="char", takefocus="False")
self.display.insert('end', "HIJKLMNOPQRSTUV")
self.display.config(state="disabled")
self.bp.pack()
self.display.pack()
def write(self):
char = self.bp.get() # should always be the "A"
print("this should be just one character: ->{}<-".format(char))
if __name__ == "__main__":
root = tkinter.Tk()
root.title("What's up here?")
App(root).pack()
root.mainloop()
What you are observing is the default behavior.您正在观察的是默认行为。 Both of those widgets (as well as the listbox) have an attribute named
exportselection
, with a default value of True
.这两个小部件(以及列表框)都有一个名为
exportselection
的属性,默认值为True
。 When True
, the widget will export the selection to be the primary selection.当
True
,小部件会将选择导出为主要选择。 On old unix systems (where tcl/tk and tkinter got its start), you could only have one "primary" selection at a time.在旧的 unix 系统(从 tcl/tk 和 tkinter 开始)上,一次只能有一个“主要”选择。
The simple solution is to set this option to False
for the text widget.简单的解决方案是将此选项设置为文本小部件的
False
。 This will allow your application to have multiple items selected at once, but only the entry widget exports the selection to the clipboard (which is required for selection_get
to work.这将允许您的应用程序一次选择多个项目,但只有条目小部件将选择导出到剪贴板(这是
selection_get
工作所必需的。
...
self.display = tkinter.Text(self, ..., exportselection=False)
...
The other issue is that on OSX the selection won't show for a disabled text widget.另一个问题是,在 OSX 上,对于禁用的文本小部件,选择不会显示。 The text is being selected, you just can't see it.
文本正在被选中,你只是看不到它。 More accurately, the selection won't show except when the widget has focus, and by default, it is not given focus when you click on it.
更准确地说,除非小部件具有焦点,否则不会显示选择,并且默认情况下,单击它时不会获得焦点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.