【问题标题】:Python tk scrollbar becomes inactive once text is outside the screen一旦文本超出屏幕,Python tk 滚动条就会变为非活动状态
【发布时间】:2013-03-05 15:23:46
【问题描述】:

每当我用文本填充框时,水平滚动条都可以正常工作,直到在屏幕上看到文本。

但是,如果行数大于屏幕上可以看到的行数,并且我向下滚动文本,则水平滚动条将变为非活动状态,直到文本太长而无法再次容纳一行时

这是我真正的基本代码:

from Tkinter import *
import Tkinter,tkFileDialog, tkFont

w = Label(root, text="Hello, world!")
w.pack()


textfr=Frame(root)
t=Text(textfr, width=100, height=10,font=("Arial",12),wrap=NONE)
t.insert('1.0', 'here is my text to insert')

Yscroll=Scrollbar(textfr, orient=VERTICAL)
Xscroll=Scrollbar(textfr, orient=HORIZONTAL)

t.configure(yscrollcommand=Yscroll.set)
t.configure(xscrollcommand=Xscroll.set)

Yscroll.config(command=t.yview)
Xscroll.config(command=t.xview)


        #pack everything

Yscroll.pack(side=RIGHT,fill=Y)
Xscroll.pack(side=BOTTOM,fill=X)
t.pack(side=LEFT, fill=BOTH, expand=TRUE)
textfr.pack(side=TOP, fill=BOTH, expand=TRUE)


root.mainloop()

唉,我找不到/谷歌正确的解决方案,即使问题很基本

有什么想法吗?

【问题讨论】:

    标签: python tkinter scrollbar


    【解决方案1】:
    from Tkinter import *
    
    class AutoScrollbar(Scrollbar):
        # a scrollbar that hides itself if it's not needed.  only
        # works if you use the grid geometry manager.
        def set(self, lo, hi):
            if float(lo) <= 0.0 and float(hi) >= 1.0:
                # grid_remove is currently missing from Tkinter!
                self.tk.call("grid", "remove", self)
            else:
                self.grid()
            Scrollbar.set(self, lo, hi)
        def pack(self, **kw):
            raise TclError, "cannot use pack with this widget"
        def place(self, **kw):
            raise TclError, "cannot use place with this widget"
    
    
    root = Tk()
    
    vscrollbar = AutoScrollbar(root)
    vscrollbar.grid(row=0, column=1, sticky=N+S)
    hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
    hscrollbar.grid(row=1, column=0, sticky=E+W)
    
    canvas = Canvas(root,
                    yscrollcommand=vscrollbar.set,
                    xscrollcommand=hscrollbar.set)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    
    vscrollbar.config(command=canvas.yview)
    hscrollbar.config(command=canvas.xview)
    
    # make the canvas expandable
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    
    #
    # create canvas contents
    
    frame = Frame(canvas)
    frame.rowconfigure(1, weight=1)
    frame.columnconfigure(1, weight=1)
    
    rows = 20
    for i in range(1,rows):
        for j in range(1,50):
            button = Button(frame, padx=7, pady=7, text="[%d,%d]" % (i,j))
            button.grid(row=i, column=j, sticky='news')
    
    canvas.create_window(0, 0, anchor=NW, window=frame)
    
    frame.update_idletasks()
    
    canvas.config(scrollregion=canvas.bbox("all"))
    
    root.mainloop()
    

    这只是一个使用按钮的简单示例。更改 for 循环的 range() 语句中的数字以了解它,您应该能够调整此代码然后适合您的代码,希望这会有所帮助。

    【讨论】:

    • 我知道应该有一些愚蠢的错误... Yscroll.pack(side=RIGHT,fill=BOTH) Xscroll.pack(side=BOTTOM,fill=BOTH) 帮助
    • @the.Legend:你不希望 fill=BOTH 用于滚动条,否则你会得到奇怪的调整大小行为。垂直滚动条应填充 Y 方向,水平滚动条应填充 X 方向。此外,作为一般经验法则,您应该使用 grid 而不是 pack 作为滚动条,否则它们看起来有点奇怪 - 在您的情况下,垂直滚动条向下延伸到水平滚动条占据的空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多