frame3= Frame(self.window) frame3.pack(fill='both', expand= 1,) scrollbar =Scrollbar(frame3) scrollbar.pack(side=RIGHT, fill=Y) self.records =Listbox(frame3,yscrollcommand=scrollbar.set) self.records.pack(fill="both", expand=1.,padx = 5.,pady = 5) scrollbar.config(command= self.records.yview) 重构这段代码
时间: 2023-11-27 12:04:47 浏览: 55
TEST 3 expand.py
可以将这段代码重构为一个函数,使其更加可复用和简洁:
```python
def create_scrollable_listbox(parent, items):
frame = Frame(parent)
frame.pack(fill='both', expand=1)
scrollbar = Scrollbar(frame)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(frame, yscrollcommand=scrollbar.set, **items)
listbox.pack(fill='both', expand=1, padx=5, pady=5)
scrollbar.config(command=listbox.yview)
return listbox
```
然后在需要使用可滚动列表框的地方,调用这个函数即可:
```python
self.records = create_scrollable_listbox(self.window, items={})
```
阅读全文