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

What is the method of displaying treeview data list in python tkinter control?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the python tkinter control treeview data list display method of what is the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python tkinter control treeview data list display method is what the article will have a harvest, let's take a look.

Material file

Result.txt

Result2.txt

The data source of the result.txt file is crawling the top 100 cat's eye movies, while the result2.txt file is only copying the content of result.txt dozens of times to make it have enough data. The following is selected:

{"Rank": "1", "title": "Farewell my Concubine", "starring": "Zhang Guorong, Zhang Fengyi, Gong Li", "release time": "1993-01-01 (Hong Kong, China)", "rating" {"ranking": "2", "title": "Roman Holiday", "starring": Gregory Parker, Audrey Hepburn Eddie Albert, release time: 1953-09-02 (USA), rating: 9.1} {Rank: 3, title: Shawshank Redemption, starring: Tim Robbins, Morgan Freeman, Bob Gunton, release time: 1994-10-14 (USA) "rating": "4", "title": "the killer is not too cold", "starring": "Jean Renault, Gary Oldman, Natalie Portman", "release time": "1994-09-14 (France)", "rating": "ranking": 5, "title": "Godfather" Starring: Marlon Brando, Al Pacino, James Ken, release time: 1972-03-24 (USA), rating: 9.3} {Rank: 6, title: Titanic, starring: Leonardo DiCaprio, Kate Winslet, Billy Zane "release time": "1998-04-03", "rating": "ranking": "7", "title": "Totoro", "starring": "Nikko, Sakamoto Chixia, Shigei Sakai", "release time": "1988-04-16 (Japan)", "rating": "ranking": "8" "title": "Tang Bohu Lian Qiu Xiang", "starring": "Zhou Xingchi, Gong Li, Zheng Peipei", "release time": "1993-07-01 (Hong Kong, China)", "rating": "ranking": "9", "title": "thousands and thousand fathom", "starring": "beauty, freedom to enter the field, summer wood truth" "release time": "2001-07-20 (Japan)", "rating": "ranking": "10", "title": "Soul-broken Blue Bridge", "starring": Vivienne, Robert Taylor, Russell Watson, release time: 1940-05-17 (USA) "rating": "11", "title": "gone with the Wind", "starring": "Vivienne, Clark Gable, Olivia de Havilan", "release time": "1939-12-15 (USA)", "rating": "9.1"}

Realize the effect

Version 1 implementation code: #-*-coding: utf-8-*-"" Created on Fri Jan 4 13:44:40 2019@author: HJY "" import tkinter as tkfrom tkinter import ttkimport reimport time # fixed pattern ='{"ranking": "(. *?)", "title": "(. *?)", "starring": "(. *?)", "release time": "(. *?)" "score": "(. *?)"}\ n'patch = re.compile (pattern) class info (): def _ _ init__ (self,): self.root = tk.Tk () self._setpage () def _ setpage (self,): start= time.time () self.scrollbar = tk.Scrollbar (self.root,) self.scrollbar.pack (side=tk.RIGHT Fill=tk.Y) title=, self.box = ttk.Treeview (self.root,columns=title, yscrollcommand=self.scrollbar.set, show='headings') self.box.column ('1mom, yscrollcommand=self.scrollbar.set, show='headings') Widthbook 50 Anchor='center') self.box.column ('2ZWH 200) self.box.column (' 3ZWT 300) self.box.column ('4Widthful 150) self.box.column (' 5ZWT 50) self.box.heading ('1') self.box.heading ('2') Text='Flim Name') self.box.heading ('3century textbook Actor') self.box.heading ('4century script textbook moment actor') self.box.heading ('5century script textbook score') self.dealline () self.scrollbar.config (command=self.box.yview) self.box.pack () tk.Label () tk.Label (self.root) Text=end-start,fg='red') .pack () tk.Button (self.root,text='Look',bg='green',) .pack () def readdata (self,): "" read the file line by line "" # read the gbk encoded file Need to add encoding='utf-8' f = open ('result.txt','r',encoding='utf-8') line = f.readline () while line: yield line line = f.readline () f.close () def dealline (self ): op = self.readdata () while 1: try: line = next (op) except StopIteration as e: break else: result = patch.match (line) self.box.insert (', 'end' Values= [result.group (I) for i in range (1Magne6)]) if _ _ name__ = ='_ main__': op = info () op.root.mainloop ()

First of all, the use of yield is introduced here to read the file line by line. The iterator only produces the next piece of data every time next (), instead of reading the whole file at once, processing each line of data in the file and saving the results. This way can effectively avoid the processing time and memory problems when facing large files.

But what's wrong with waiting for the data in the file to be processed and inserted into the tkinter control before executing the next step of the program (that is, the program statement after self.dealline ())? If you are dealing with files like result.txt files with only 100 pieces of data, users will not feel anything, but if you deal with files like result2.txt, you will feel stuttered, as if it will take a while to display the application.

Solution idea

Can you insert only 10 or 50 pieces of data into the control at the beginning, and load the next 10 pieces of data as soon as the user browses to the 10th piece of data?

Implementation 1: bind the wheel event of the mouse, once the scroll down event is heard, it will trigger the load.

Implementation 2: when the user clicks the button, the data is loaded. This is generally used for turning pages and so on.

Implementation 3: when the user drags the slider to the end, if there is still data to be loaded, the load will be triggered (for implementation).

The improved code implements #-*-coding: utf-8-*-"" Created on Tue Jan 8 13:45:21 2019@author: HJY "" #-*-coding: utf-8-*-"Created on Fri Jan 4 13:44:40 2019@author: HJY"import tkinter as tkfrom tkinter import ttk import reimport time # fixed pattern ='{" ranking ":" (. *?) "," title ":" (. *?) " "starring": (. *?), release time: (. *?), rating: (. *?)}\ n'patch = re.compile (pattern) class info (): def _ _ init__ (self,): self.root = tk.Tk () self._setpage () def _ setpage (self) ): start= time.time () self.scrollbar = tk.Scrollbar (self.root,command=self.moveScroll) self.scrollbar.bind ("", self.moveScroll) self.scrollbar.pack (side=tk.RIGHT,fill=tk.Y) title= [] self.box = ttk.Treeview (self.root,columns=title) Yscrollcommand=self.scrollbar.set, show='headings') self.box.bind ("", self.moveScroll) self.box.column ('1Widthwriting 50)) self.box.column (' 2Widthwriting 200) self.box.column ('3Widthwriting 300) (anchor='center') self.box.column ('4century jewellery 150 miner anchor Name'') self.box.column ('5century recorder 50 pedagogical anchorboat circle center') self.box.heading (' 1Zongdongyuange`) self.box.heading ('2yutemparting textbook Flim Name') self.box.heading (' 3century textbook actor') self.box.heading ('4') Text='Time') self.box.heading ('5percent textbook processing score') # object handling self.op = self.readdata () self.dealline (self.op) self.scrollbar.config (command=self.box.yview) self.box.pack () end=time.time () tk.Label (self.root,text=end-start Fg='red') .pack () tk.Button (self.root,text='Look',bg='green',command=self.turn) .pack () # page flip mode Each click loads 10 more data def turn (self): # self.scrollbar.set (0.89, 0.99) # print (self.scrollbar.get ()) self.dealline (self.op) # mouse scrolling mode Load data def moveScroll (self,event): if event.delta < 0: self.dealline (self.op) def dragScroll (self): # unimplemented pass def readdata (self,): "" read files line by line "# read gbk encoded files Need to add encoding='utf-8' f = open ('result2.txt','r',encoding='utf-8') line = f.readline () while line: yield line line = f.readline () f.close () def dealline (self Op): self.cal = 0 while 1: try: line = next (op) except StopIteration: break else: result = patch.match (line) self.box.insert ('', 'end' Values= [result.group (I) for i in range (1Magne6)]) self.cal + = 1 if self.cal = = 10: break if _ _ name__ = ='_ main__': op = info () op.root.mainloop ()

Commentary

The problem with this model:

1. If the data is used to search, and the user does not trigger the load, and the data to be searched is not in the loaded data, then the search will not be available. Of course, if the search is not based on the data in the control, but on the file itself or the database, and so on, there is no need for this consideration. In addition, if you want to ask for an exhibition

Shows the row in which the searched data is located, so you need to load all unloaded data up to the searched data as soon as the searched data row is not loaded.

2. When the wheel rolls down, the user triggers the loading before turning the data to the bottom data.

New ideas

You can use the get () method of the scrollbar control to get the position of the slider. Once the bottom position of the slider is 1, the slider is already at the bottom, and loading is triggered, and because the next () iterator will trigger a stopiteration exception to prevent loading when there is no data, and load if there is any more data. We can implement this process as a function, bound to the command property of the scrollbar control, so that simply sliding the slider will trigger the function call.

But we have bound it with the yview function of the treeview control to realize the slider to slide the list box, so we need to embed our own implementation into the yview function, or the yview function into the function we implemented, but some of the middle links can only be done by understanding the processing mode of the yview function.

This is the end of the article on "what is the method of displaying the treeview data list of python tkinter controls?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the method of displaying python tkinter control treeview data list". If you want to learn more, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report