Python-Tkinter Treeview滚动条用法代码示例

2021年4月1日16:49:38 发表评论 1,858 次浏览

Python提供了几种用于构建GUI和python tkinter是其中之一。这是标准图形用户界面用于Python的库, 可帮助轻松制作GUI应用程序。它提供了一个高效的面向对象的界面tkGUI工具箱。它还具有多个控件, 称为控件, 例如文本框, 滚动条, 按钮等。此外, Tkinter具有一些几何管理方法, 即pack(), grid()和place()这对组织小部件很有帮助。

注意:有关更多信息, 请参阅Python GUI – Tkinter

树形视图滚动条

当滚动条使用树状视图小部件时,这种类型的滚动条称为树状视图滚动条。其中,treeview小部件有助于以列的形式在树的右侧显示树中列出的每个项目的多个特性。但是,它可以在python中使用tkinter实现,并借助tkinter支持的一些小部件和几何管理方法。

下面的例子说明了使用Python-tkinter使用树状视图滚动条的用法:

范例1:

# Python program to illustrate the usage of 
# treeview scrollbars using tkinter
  
  
from tkinter import ttk
import tkinter as tk
  
# Creating tkinter window
window = tk.Tk()
window.resizable(width = 1 , height = 1 )
  
# Using treeview widget
treev = ttk.Treeview(window, selectmode = 'browse' )
  
# Calling pack method w.r.to treeview
treev.pack(side = 'right' )
  
# Constructing vertical scrollbar
# with treeview
verscrlbar = ttk.Scrollbar(window, orient = "vertical" , command = treev.yview)
  
# Calling pack method w.r.to verical 
# scrollbar
verscrlbar.pack(side = 'right' , fill = 'x' )
  
# Configuring treeview
treev.configure(xscrollcommand = verscrlbar. set )
  
# Defining number of columns
treev[ "columns" ] = ( "1" , "2" , "3" )
  
# Defining heading
treev[ 'show' ] = 'headings'
  
# Assigning the width and anchor to  the
# respective columns
treev.column( "1" , width = 90 , anchor = 'c' )
treev.column( "2" , width = 90 , anchor = 'se' )
treev.column( "3" , width = 90 , anchor = 'se' )
  
# Assigning the heading names to the 
# respective columns
treev.heading( "1" , text = "Name" )
treev.heading( "2" , text = "Sex" )
treev.heading( "3" , text = "Age" )
  
# Inserting the items and their features to the 
# columns built
treev.insert(" ", 'end', text =" L1", values = ( "Nidhi" , "F" , "25" ))
treev.insert(" ", 'end', text =" L2", values = ( "Nisha" , "F" , "23" ))
treev.insert(" ", 'end', text =" L3", values = ( "Preeti" , "F" , "27" ))
treev.insert(" ", 'end', text =" L4", values = ( "Rahul" , "M" , "20" ))
treev.insert(" ", 'end', text =" L5", values = ( "Sonu" , "F" , "18" ))
treev.insert(" ", 'end', text =" L6", values = ( "Rohit" , "M" , "19" ))
treev.insert(" ", 'end', text =" L7", values = ( "Geeta" , "F" , "25" ))
treev.insert(" ", 'end', text =" L8", values = ( "Ankit" , "M" , "22" ))
treev.insert(" ", 'end', text =" L10", values = ( "Mukul" , "F" , "25" ))
treev.insert(" ", 'end', text =" L11", values = ( "Mohit" , "M" , "16" ))
treev.insert(" ", 'end', text =" L12", values = ( "Vivek" , "M" , "22" ))
treev.insert(" ", 'end', text =" L13", values = ( "Suman" , "F" , "30" ))
  
# Calling mainloop
window.mainloop()

输出如下:

在上面的程序中, 我们使用了pack()几何管理方法的方法。并且, 我们根据代码的要求仅构建了垂直滚动条, 但是你可以根据你的要求构建这两个条。此外, 此处使用锚点来定义文本的位置。但是, 你也可以使用其他几何管理方法来构造树视图滚动条。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: