Python 3.x Python Tkinter可滚动文本,滑块不';不出现

Python 3.x Python Tkinter可滚动文本,滑块不';不出现,python-3.x,text,tkinter,scrollbar,Python 3.x,Text,Tkinter,Scrollbar,我正在尝试用tkinter小部件制作一个可滚动的文本。我希望滚动条仅在需要时显示(当我的文本小部件的一部分不可见时)。 我的程序会搜索每一次键入,如果是,滚动条会出现,如果不是,滚动条不会出现。 第一次它工作得很好,但是如果我删除一些文本(因此滚动条消失),然后写一些,滚动条就会出现,但没有滑块 #-*-coding:latin-1-* from tkinter import * class TextScrollbar(Frame): """ A Text wi

我正在尝试用tkinter小部件制作一个可滚动的文本。我希望滚动条仅在需要时显示(当我的文本小部件的一部分不可见时)。
我的程序会搜索每一次键入,如果是,滚动条会出现,如果不是,滚动条不会出现。
第一次它工作得很好,但是如果我删除一些文本(因此滚动条消失),然后写一些,滚动条就会出现,但没有滑块

   #-*-coding:latin-1-*

from tkinter import *


class TextScrollbar(Frame):
    """
       A Text widget which can be scrolled.
       Text widget with a scrollbar appearing only when you need it
       (when there is text that you can see)
       Use self.Text to acccess to your Text widget
    """

    def __init__( self, master=None, cnf={}, **kw ):

        #Creat a Frame which will contain the Text and the Scrollbar widget
        Frame.__init__( self, master=None, cnf={}, **kw )

        #Creat Scrollbar widget
        self.ScrollBar=Scrollbar( self, orient='vertical' )

        #Creat Text widget
        self.Text=Text(self, cnf={}, **kw)

        #Link between Text and Scrollbar widgets
        self.Text.config( yscrollcommand=self.ScrollBar.set )
        self.ScrollBar.config( command=self.Text.yview )      

        #Distribution of the Text widget in the frame
        self.Text.pack( side='left', fill=BOTH, expand=1 )


        def _typing(event):
            """Check whether you need a scrollbar or not"""
            if Text.ScrollBar.get()==(0.0, 1.0):
                self.ScrollBar.pack_forget()
            else:
                self.ScrollBar.pack( side='right', fill=Y, expand=1 )

        self.Text.bind('<Key>',_typing)



root=Tk()
Text=TextScrollbar(root)
Text.pack(fill=BOTH, expand=1)
#-*-编码:拉丁语-1-*
从tkinter进口*
类文本滚动条(框架):
"""
可以滚动的文本小部件。
带有滚动条的文本小部件,仅在需要时显示
(当您可以看到文本时)
使用self.Text访问文本小部件
"""
def uuu init uuuu(self,master=None,cnf={},**kw):
#创建一个包含文本和滚动条小部件的框架
帧。uuu init_uuu(self,master=None,cnf={},**kw)
#创建滚动条小部件
self.ScrollBar=滚动条(self,orient='vertical')
#创建文本小部件
Text=Text(self,cnf={},**kw)
#文本和滚动条小部件之间的链接
self.Text.config(yscrollcommand=self.ScrollBar.set)
self.ScrollBar.config(命令=self.Text.yview)
#文本小部件在框架中的分布
self.Text.pack(side='left',fill=BOTH,expand=1)
def_类型(事件):
“”“检查是否需要滚动条”“”
如果Text.ScrollBar.get()==(0.0,1.0):
self.ScrollBar.pack\u-forget()
其他:
self.ScrollBar.pack(side='right',fill=Y,expand=1)
self.Text.bind(“”,_键入)
root=Tk()
Text=文本滚动条(根)
Text.pack(填充=两者,展开=1)

我仍然不知道为什么它不起作用,但将.pack methode替换为.grid methode它起作用了,下面是更新的代码

    #-*-coding:latin-1-*

from tkinter import *


class TextScrollbar(Frame):
    """
       A Text widget which can be scrolled.
       Text widget with a scrollbar appearing only when you need it
       (when there is text that you can see)
       Use self.Text to acccess to your Text widget
    """

    def __init__( self, master=None, cnf={}, **kw ):

        #Creat a Frame which will contain the Text and the Scrollbar widget
        Frame.__init__( self, master=None, cnf={}, **kw )
        self.grid_columnconfigure( 0, weight=1 )
        self.grid_rowconfigure( 0, weight=1 )

        #Creat Scrollbar widget
        self.Scrollbar=Scrollbar( self, orient='vertical' )

        #Creat Text widget
        self.Text=Text( self, cnf={}, **kw )

        #Link between Text and Scrollbar widgets
        self.Text.config( yscrollcommand=self.Scrollbar.set )
        self.Scrollbar.config( command=self.Text.yview )      

        #Distribution of the Text widget in the frame
        self.Text.grid( row=0, column=0, sticky=N+S+E+W )



        def TypingAndResizing(event):
            """Check whether you need a scrollbar or not"""
            if Text.Scrollbar.get()==(0.0, 1.0):
                self.Scrollbar.grid_forget()
            else:
                self.Scrollbar.grid( row=0, column=1, sticky=S+N )

        self.Text.bind( '<KeyRelease>', TypingAndResizing )
        self.Text.bind( '<Configure>', TypingAndResizing )



root=Tk()
Text=TextScrollbar(root)
Text.pack(fill=BOTH, expand=1)
#-*-编码:拉丁语-1-*
从tkinter进口*
类文本滚动条(框架):
"""
可以滚动的文本小部件。
带有滚动条的文本小部件,仅在需要时显示
(当您可以看到文本时)
使用self.Text访问文本小部件
"""
def uuu init uuuu(self,master=None,cnf={},**kw):
#创建一个包含文本和滚动条小部件的框架
帧。uuu init_uuu(self,master=None,cnf={},**kw)
self.grid\u column配置(0,权重=1)
self.grid_rowconfigure(0,权重=1)
#创建滚动条小部件
self.Scrollbar=滚动条(self,orient='vertical')
#创建文本小部件
Text=Text(self,cnf={},**kw)
#文本和滚动条小部件之间的链接
self.Text.config(yscrollcommand=self.Scrollbar.set)
self.Scrollbar.config(命令=self.Text.yview)
#文本小部件在框架中的分布
self.Text.grid(行=0,列=0,粘滞=N+S+E+W)
def键入和重新设置(事件):
“”“检查是否需要滚动条”“”
如果Text.Scrollbar.get()==(0.0,1.0):
self.Scrollbar.grid_忘记()
其他:
self.Scrollbar.grid(行=0,列=1,粘性=S+N)
self.Text.bind(“”,键入和缩放)
self.Text.bind(“”,键入和缩放)
root=Tk()
Text=文本滚动条(根)
Text.pack(填充=两者,展开=1)

我终于找到了解决办法。而不是.pack,我使用.grid。
它不仅解决了问题,而且更好地处理树脂

你确定这是你的真实代码吗<代码>行、
粘性
对于
包装
无效。事实上,这是因为我尝试了网格,但忘记了更改。它完成了,并且正确的代码被更新了。您的绑定发生在插入新字符之前,因此它总是会有一点延迟。如果您使用
而不是
绑定,它是否工作得更好?感谢这一技巧,它非常有用,使我的程序变得更好,但它并不能解决我的问题。