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

The realization method of Tkinter scroll bar Scrollbar

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

Share

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

This article introduces the relevant knowledge of "the implementation of Tkinter scroll bar Scrollbar". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First build a label control to display information:

Label = Label (root, width = 50) label.grid (row=0, column=0, columnspan=2)

Next, build the first Scrollbar control. If nothing is done, the user can drag the slider, but once the mouse is released, the slider will return to its original position. To solve this problem, an on_scroll function is implemented in the code.

Def on_scroll (* args): pos = scroll_bar.get () print (pos) page = 0.1 if args [0] = = 'scroll': if args [2] = =' units': first = pos [0] + int (args [1]) * 0.01 elif args [2] = 'pages': first = pos [0] + int (args [1]) * 0.1 Else: first = 0 elif args [0] = 'moveto': first = float (args [1]) else: first = 0 scroll_bar.set (first) First + 0.1) label.config (text=str (args))

Scroll_bar = Scrollbar (root, orient = HORIZONTAL, command=on_scroll) scroll_bar.grid (row = 1, column=0, columnspan=2, sticky=W+E)

The function first determines the parameters of on_scroll. If a parameter is' scroll', it means that this pass is a user's scrolling operation, and scrolling is divided into two types: one is the scrolling in units of 'unit' when the mouse clicks on the small triangle at both ends, and the other is the scrolling in units of' page' when the mouse clicks on an empty area outside the slider. The middle parameter can be 1 or-1 to indicate the direction of scrolling.

If the user drags the slider directly, an 'moveto' event is generated, whose argument is the position of the slider.

After on_scroll handles the event information properly, the set method of Scrollbar is called to specify the start position and the end position of the slider respectively in order to realize the function of moving the slider according to the operation. It is important to note that Scrollbar does not provide the ability to specify a scrolling range, which is fixed as [0J1].

Although you can use Scrollbar to achieve scrolling in this way, you have to write your own code to handle the corresponding scrolling events each time, which is a tedious process. So we introduced the concept of class for the first time in this series, and after a simple encapsulation of the scrollbar function, we provided a ScrollbarEx class:

Class ScrollbarEx (Scrollbar): def _ _ init__ (self, master) * * args): arg_dict = copy.copy (args) try: self.min = arg_dict.pop ('min') except KeyError: self.min = 0 try: self.max = arg_dict.pop (' max') except KeyError: self.max = 1 try: self. Page = arg_dict.pop ('page') except KeyError: self.page = 0.1try: self.unit = arg_dict.pop (' unit') except KeyError: self.unit = 0.01try: self.on_scroll = arg_dict.pop ('on_scroll') except KeyError: pass Arg_dict.update (command = self.scroll_handler) Scrollbar.__init__ (self Master, arg_dict,) self.scroll_handler ()

Def scroll_handler (self * args): pos = self.get () span = self.max-self.min page = self.page / span unit = self.unit / span first = 0 if args: if args [0] = = 'scroll': if args [2] =' units': first = pos [0] + int (args [1]) * Unit elif args [2] = 'pages': first = pos [0] + int (args [1]) * page else: first = 0 elif args [0] =' moveto': first = float (args [1]) else: first = 0 self.set (first) First + page) if self.on_scroll: self.on_scroll (int (self.min + first * span), int (self.min + first * span + self.page))

What ScrollBarEx does is no different from the scrolling event handling described earlier, only to simplify the user's usage process. With this class, users can use the scroll bar function in the same way as Scrollbar:

Def on_scroll_ex (first, last): label.config (text= "first= {}, last= {}" .format (first, last))

Scrollbar_ex = ScrollbarEx (root, min=0, max=100, page=10, unit=2, on_scroll=on_scroll_ex, orient = HORIZONTAL) scrollbar_ex.grid (row=2, column=0, columnspan=2, sticky=W+E)

What the user needs to do is to specify the scrolling range, page scrolling distance, unit scrolling distance and other information through several singular numbers of min,max,page,unit without paying attention to the details of event handling.

This is the end of the content of "the implementation of Tkinter scroll bar Scrollbar". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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