📜  Tkinter 中的可滚动帧

📅  最后修改于: 2022-05-13 01:54:50.009000             🧑  作者: Mango

Tkinter 中的可滚动帧

滚动条是一个小部件,可用于滚动另一个小部件中的文本。例如,可以使用滚动条从上到下或从左到右滚动 Text、Canvas Frame 或 Listbox 中的文本。有两种类型的滚动条。它们是水平的和垂直的。水平滚动条对于从左到右查看文本很有用。垂直滚动条对于从上到下滚动文本很有用。
现在让我们看看如何创建滚动条。要创建滚动条,我们必须创建一个滚动条类对象,如下所示:

h = Scrollbar(root, orient='horizontal')

这里 h 表示作为根窗口的子级创建的滚动条对象。这里 orient 表示水平滚动条的水平,垂直表示垂直滚动条。类似地创建一个垂直滚动条,我们可以这样写:

v = Scrollbar(root, orient='vertical')

在将滚动条附加到文本框、列表框等小部件时,我们必须为水平滚动条添加命令xview ,为垂直滚动条添加命令yview

h.config(command=t.xview) #for horizontal scrollbar
v.config(command=t.yview) #for vertical scrollbar

现在让我们看看将垂直和水平滚动条附加到 Text Widget 的代码。

Python3
# Python Program to make a scrollable frame
# using Tkinter
  
from tkinter import *
  
class ScrollBar:
     
    # constructor
    def __init__(self):
         
        # create root window
        root = Tk()
  
        # create a horizontal scrollbar by
        # setting orient to horizontal
        h = Scrollbar(root, orient = 'horizontal')
  
        # attach Scrollbar to root window at
        # the bootom
        h.pack(side = BOTTOM, fill = X)
  
        # create a vertical scrollbar-no need
        # to write orient as it is by
        # default vertical
        v = Scrollbar(root)
  
        # attach Scrollbar to root window on
        # the side
        v.pack(side = RIGHT, fill = Y)
          
  
        # create a Text widget with 15 chars
        # width and 15 lines height
        # here xscrollcomannd is used to attach Text
        # widget to the horizontal scrollbar
        # here yscrollcomannd is used to attach Text
        # widget to the vertical scrollbar
        t = Text(root, width = 15, height = 15, wrap = NONE,
                 xscrollcommand = h.set,
                 yscrollcommand = v.set)
  
        # insert some text into the text widget
        for i in range(20):
            t.insert(END,"this is some text\n")
  
        # attach Text widget to root window at top
        t.pack(side=TOP, fill=X)
  
        # here command represents the method to
        # be executed xview is executed on
        # object 't' Here t may represent any
        # widget
        h.config(command=t.xview)
  
        # here command represents the method to
        # be executed yview is executed on
        # object 't' Here t may represent any
        # widget
        v.config(command=t.yview)
  
        # the root window handles the mouse
        # click event
        root.mainloop()
 
# create an object to Scrollbar class
s = ScrollBar()


输出: