Tkinter 小部件上的垂直和水平滚动条
滚动条有助于为应用程序提供动态行为。在 Tkinter 应用程序中,我们可以创建垂直滚动条和水平滚动条。滚动条是通过初始化Scrollbar() 小部件的对象创建的。
要创建一个水平滚动条,我们必须提供方向,即“水平”或“垂直”。一旦我们使用滚动条对特定小部件进行配置,就可以访问滚动条。
示例
#Import the required libraries
from tkinter import *
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("700x350")
#Create some dummy Text
text_v = "Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming."
text_h = ("\nNASA \n Google \nNokia \nFacebook \n Netflix \n Expedia \n Reddit \n Quora \n MIT\n Udemy \n Shutterstock \nSpotify\nAmazon\nMozilla\nDropbox")
#Add a Vertical Scrollbar
scroll_v = Scrollbar(win)
scroll_v.pack(side= RIGHT,fill="y")
#Add a Horizontal Scrollbar
scroll_h = Scrollbar(win, orient= HORIZONTAL)
scroll_h.pack(side= BOTTOM, fill= "x")
#Add a Text widget
text = Text(win, height= 500, width= 350, yscrollcommand= scroll_v.set,
xscrollcommand = scroll_h.set, wrap= NONE, font= ('Helvetica 15'))
text.pack(fill = BOTH, expand=0)
text.insert(END, text_v)
text.insert(END, text_h)
#Attact the scrollbar with the text widget
scroll_h.config(command = text.xview)
scroll_v.config(command = text.yview)
win.mainloop()输出
运行上面的代码将显示一个窗口,其中包含有关 Python 编程语言的上下文。可以使用水平和垂直滚动条动态查看上下文。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP