Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate scrollable function with scrolling function by Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to generate scrollable functions with scrolling controls by Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Python generates scrollable functions with scrolling controls.

When the program reaches a certain size, you often want to build controls with scrolling capabilities. For example, in the FileBrowser in the following figure, both the TreeView on the left and the ListView on the right have horizontal and vertical Scrollbar controls.

The code to generate a control with Scrollbar in Tkinter is roughly as follows:

# generate Frame container to hold TreeView,Scrollbartree_area = Frame (paned_window) # set horizontal stretch tree_area.grid_rowconfigure (0, weight=1) # set vertical stretch tree_area.grid_columnconfigure (0, weight=1) # generate TreeViewtree_view = Treeview (tree_area, show='tree', selectmode='browse') # set TreeView layout position tree_view.grid (row=0, column=0, sticky='nsew') # generate vertical scroll bar scroll_ty = Scrollbar (tree_area, orient=VERTICAL) Command=tree_view.yview) # set scroll bar layout position scroll_ty.grid (row=0, column=1, sticky=N+S) # associated scroll action tree_view ['yscrollcommand'] = scroll_ty.set# generate horizontal scroll bar scroll_tx = Scrollbar (tree_area, orient=HORIZONTAL, command=tree_view.xview) # set scroll bar layout position scroll_tx.grid (row=1, column=0, sticky=E+W) # associated scroll action tree_view [' xscrollcommand'] = scroll_tx.set

Although the logic is clear, it is always troublesome to repeat the same thing many times, so the author designs the following scrollable function to simplify the process:

Def scrollable (master, w_type, * * kwargs): # get SizeGrip setting content size_grip = kwargs.get ('size_grip') # remove' size_grip' setting if size_grip: kwargs.pop ('size_grip') # build Frame control frame = Frame (master) # set control horizontal stretch frame.grid_rowconfigure (0 Weight=1) # set control vertical stretch frame.grid_columnconfigure (0, weight=1) # build control widget = w_type (frame, * * kwargs) # set control layout widget.grid (row=0, column=0, sticky='nsew') # build vertical Scrollbar scroll_y = Scrollbar (frame, orient=VERTICAL, command=widget.yview) # set control layout scroll_y.grid (row=0, column=1 Sticky=N + S) # bind scroll action widget ['yscrollcommand'] = scroll_y.set # build horizontal Scrollbar scroll_x = Scrollbar (frame, orient=HORIZONTAL, command=widget.xview) # set control layout scroll_x.grid (row=1, column=0, sticky=E + W) # bind scroll action widget [' xscrollcommand'] = scroll_x.set # generate Sizegrip control if size_grip: Sizegrip (frame) .grid (row=1 Column=1) return frame,widget

Although the content is basically the same, only a simple package, but the effect is obvious: the code to build the same scrolling Treeview has changed from more than 20 lines to 1 line!

Tree_area, tree_view = scrollable (paned_window, Treeview, show='tree', selectmode='browse')

Another advantage is that there is no need to know the use of Scrollbar when using scrollable.

At this point, I believe you have a deeper understanding of "how Python generates scrollable functions with scrolling controls". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report