import requests from bs4 import BeautifulSoup import tkinter as tk from tkinter import…
- User
import requests from bs4 import BeautifulSoup import tkinter as tk from tkinter import ttk
Create a function to scrape the website and extract the second table tag
def get_table_data(): # Send a GET request to the website response = requests.get("http://10.29.70.115:8080/apex/f?p=102:2::::::") # Use BeautifulSoup to parse the HTML content soup = BeautifulSoup(response.content, "html.parser") # print(soup) # Find all table tags and extract the second one table = soup.find_all("table")[1] # Extract the data from the table and return it data = [] for row in table.find_all("tr"): row_data = [] for cell in row.find_all("th"): row_data.append(cell.text) for cell in row.find_all("td"): row_data.append(cell.text) data.append(row_data) return data
Create a function to display the table data in a tkinter window
def display_table_data(): # Create a new tkinter window window = tk.Tk() # Set the window title window.title("Table Data") # Set the window size # window.geometry("1000x800") # Get the table data table_data = get_table_data() # Create a tkinter frame to hold the treeview and scrollbar frame = tk.Frame(window) frame.pack(fill=tk.BOTH, expand=True) # Create a tkinter treeview for the table data # treeview = ttk.Treeview(frame, columns=range(len(table_data[0])), show="headings") treeview = ttk.Treeview(frame, columns=table_data[0], show="headings") print(table_data) print(table_data[0]) print(len(table_data[0])) print(range(len(table_data[0]))) # Add each column of data to the treeview # for i, column in enumerate(table_data[0]): # treeview.heading(i, text=column) # treeview.column(i, width=1000, anchor='center') # Add each row of data to the treeview for row in table_data[0:]: treeview.insert("", tk.END, values=row) # Create a tkinter scrollbar for the treeview scrollbar = tk.Scrollbar(frame) # Link the scrollbar to the treeview treeview.config(yscrollcommand=scrollbar.set) scrollbar.config(command=treeview.yview) # scrollbar.config(yscrollincrement=1) # Pack the treeview and scrollbar into the frame treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Run the tkinter event loop window.mainloop()
Call the display_table_data function to show the table data in a tkinter window
display_table_data() 很卡如何优化一起加载所有行
- Assistant
可以考虑使用virtual events来延迟加载数据,只有即将可视时才加载相应的行数据,并将已经不可视的行数据释放。另外可以考虑对数据量较大的表格进行分页,一页只显示部分行数据,通过翻页切换不同的行数据。也可以使用更高效的GUI库,如PyQt或wxPython来优化显示效率。
- User
具体代码如何更改
- Assistant
这里给出一个根据当前滚动位置更新Treeview数据的示例代码:
import requests from bs4 import BeautifulSoup import tkinter as tk from tkinter import ttk # 设置每次加载的行数 BATCH_SIZE = 50 class VirtualEventsTreeview(ttk.Treeview): def __init__(self, *args, **kwargs): ttk.Treeview.__init__(self, *args, **kwargs) self.last_range = (0, 0) # 记录已经加载的行的范围 def set_data(self, rows): self._rows = rows def update(self, low, high): current_range = set(range(low, high)) last_range = set(range(self.last_range[0], self.last_range[1])) new_rows = current_range - last_range old_rows = last_range - current_range for row_no in old_rows: self.detach(self._rows[row_no]) for row_no in new_rows: self.insert("", "end", values=self._rows[row_no]) self.last_range = (low, high) def get_table_data(): # Send a GET request to the website response = requests.get("http://10.29.70.115:8080/apex/f?p=102:2::::::") # Use BeautifulSoup to parse the HTML content soup = BeautifulSoup(response.content, "html.parser") # print(soup) # Find all table tags and extract the second one table = soup.find_all("table")[1] # Extract the data from the table and return it data = [] for row in table.find_all("tr"): row_data = [] for cell in row.find_all("th"): row_data.append(cell.text) for cell in row.find_all("td"): row_data.append(cell.text) data.append(row_data) return data def display_table_data(): # Create a new tkinter window window = tk.Tk() # Set the window title window.title("Table Data") # Set the window size # window.geometry("1000x800") # Create a tkinter frame to hold the treeview and scrollbar frame = tk.Frame(window) frame.pack(fill=tk.BOTH, expand=True) # Get the table data table_data = get_table_data()[1:] # 去掉第一行 # Create a tkinter scrollbar for the treeview scrollbar = tk.Scrollbar(frame) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Create a virtual events treeview for the table data treeview = VirtualEventsTreeview(frame, columns=table_data[0], show="headings", yscrollcommand=scrollbar.set) treeview.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) treeview.set_data(table_data) n_items = len(table_data) n_batches = (n_items+BATCH_SIZE-1) // BATCH_SIZE treeview.update(0, BATCH_SIZE) def on_scroll(*args): fraction = float(args[0]) low = int(fraction * n_items) high = min(low + BATCH_SIZE, n_items) treeview.update(low, high) scrollbar.config(command=on_scroll) # Run the tkinter event loop window.mainloop() # Call the display_table_data function to show the table data in a tkinter window display_table_data()这里我们使用了一个新的类
VirtualEventsTreeview来继承ttk.Treeview,并覆盖了update方法来根据传入的low和high参数更新显示的行数据。在display_table_data函数中,我们根据下方的拖动条的滑动位置来更新VirtualEventsTreeview的行数据显示。我们将总行数n_items划分为若干个批次,每个批次载入BATCH_SIZE行数据。每次更新所需显示的行数据范围时,仅仅导入当前滑动位置范围内的数据,并在此基础上插入新行数据,移除旧行数据,以减少内存占用和提高性能。