【问题标题】:Python Tkinter Scrollable Text, the slider doesn't appearPython Tkinter 可滚动文本,滑块不出现
【发布时间】:2016-02-28 11:36:25
【问题描述】:

我正在尝试使用 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)

First trial

Second trial

我仍然不知道为什么它不起作用,但是将 .pack 方法替换为 .grid 方法它可以工作,这里是更新的代码

    #-*-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)

【问题讨论】:

  • 您确定这是您的真实代码吗? rowcolumnsticky 不是 pack 的有效选项。
  • 事实上这是因为我尝试使用网格但我忘记了改变。大功告成,更新了正确的代码
  • 你的绑定发生在新字符插入之前,所以它总是会滞后一点。如果绑定&lt;KeyRelease&gt; 而不是&lt;Key&gt;,效果会更好吗?
  • 感谢这个技巧,它很有用,让我的程序变得更好,但它并没有解决我的问题。

标签: python-3.x text tkinter scrollbar


【解决方案1】:

我终于找到了解决办法。我使用 .grid 代替 .pack。 它不仅解决了问题,而且处理调整大小也更好!

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多