简体   繁体   English

我无法在文本标签-Tkinter 的文本中使用键盘滚动

[英]I can't scroll with the keyboard in the text of my Text tag -Tkinter

I basically got the unwanted characters removed from the Text widget, but when I hit a line break and try to scroll the text with the keyboard or mouse, I just can't (it always stays in the same place).我基本上从文本小部件中删除了不需要的字符,但是当我遇到换行符并尝试使用键盘或鼠标滚动文本时,我就是做不到(它总是停留在同一个地方)。 this is done in the "validate text" method这是在“验证文本”方法中完成的


class NewNewsFrame(Frame):
    def __init__(self, parent):
        self.Parent = parent
        self.initializecomponents()
        pass

    def validate_text(self, widger):
        value = widger.get("1.0", "end-1c")
        print value.fon
        if not value.isalnum():
            var = str()
            for i in value:
                print(f"({i})")
                var += i if(i.isalpha() or i.isdigit() or i == "(" or i == ")" or i == " " or i == "," or i == "." or i == "\n")else ""
            widger.delete("1.0", "end-1c")
            widger.insert("end-1c", var)
        pass

    def initializecomponents(self):
        Frame.__init__(self, self.Parent)

        self.DescriptionLabel = Label(self)
        self.DescriptionBox = Text(self)

        # DescriptionLabel
        self.DescriptionLabel.config(text="Description of the news:",bg=self["bg"], fg="#FFFFFF", font="none 15",anchor="nw")
        self.DescriptionLabel.place(relwidth=1, relheight=0.1, relx=0, rely=0.11, anchor="nw")

        # DescriptionBox
        self.DescriptionBox.bind("<KeyPress>", lambda event: self.validate_text(self.DescriptionBox))
        self.DescriptionBox.bind("<FocusOut>", lambda event: self.validate_text(self.DescriptionBox))

        self.DescriptionBox.place(relheight=0.4, relwidth=1, relx=0, rely=0.16, anchor="nw")
    pass

I tried to find how keyboard scrolling works, but I still don't know how to do it我试图找到键盘滚动的工作原理,但我仍然不知道该怎么做

The problem is that you're deleting and restoring all of the text with every keypress.问题是您每次按键都会删除和恢复所有文本。 This causes the cursor position to change in unexpected ways that breaks the default bindings.这会导致光标位置以破坏默认绑定的意外方式发生变化。

If you're wanting to prevent certain characters from being entered, there's a better way.如果您想阻止输入某些字符,有更好的方法。 If your validation function returns the string "break", that prevents the character from being inserted.如果您的验证函数返回字符串“break”,则会阻止插入该字符。 You don't have to re-scan the entire contents or delete and restore the text, because the bad characters never get entered in the first place.您不必重新扫描整个内容或删除和恢复文本,因为坏字符永远不会首先输入。

Your validation function might look something like this:您的验证函数可能如下所示:

def validate_text(self, event):
    if event.char.isalpha() or event.char.isdigit() or event.char in "() ,.\n":
        pass
    else:
        return "break"

Next, simplify the binding to look like the following.接下来,将绑定简化为如下所示。 Tkinter will automatically pass the event parameter to the function. Tkinter 会自动将event参数传递给函数。

self.DescriptionBox.bind("<KeyPress>", self.validate_text)

This will break your <FocusOut> binding, but I'm not sure it's needed.这将破坏您的<FocusOut>绑定,但我不确定是否需要它。

For more information about how events are processed and why returning "break" does what it does, see this answer to the question Basic query regarding bindtags in tkinter .有关如何处理事件以及返回“break”的原因的更多信息,请参阅问题Basic query regarding bindtags in tkinter 的答案 That question is about an Entry widget but the concept is the same for all widgets.该问题与Entry小部件有关,但所有小部件的概念都是相同的。

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

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