简体   繁体   English

选择从 ttk.Entry 移动到 tkinter.Text

[英]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

  1. When you run the program, you can see the "A" highlighted in the Entry (upper) box.当您运行程序时,您可以看到“条目”(上)框中突出显示的“A”。
  2. Push the "Write Data" button a few times;按几次“写入数据”按钮; the print statement will display the "A" that's selected in the Entry box.打印语句将显示在输入框中选择的“A”。
  3. Sweep the mouse over some text in the Text (lower) box;将鼠标滑过文本(下方)框中的一些文本; it won't be highlighted, but the highlighting in the Entry will disappear.它不会突出显示,但条目中的突出显示将消失。
  4. Push the "Write Data" button;按下“写入数据”按钮; the print statement will display the characters you swept with the mouse.打印语句将显示您用鼠标扫过的字符。
  5. Those characters came from selection_get() on the Entry!这些字符来自 Entry 上的 selection_get()! You can tell that it got them from the Text because the two boxes have no characters in common.您可以看出它是从 Text 中获取的,因为这两个框没有共同的字符。

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.

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