python - 当列表溢出时启用滚动条,当列表正常时禁用滚动条并将滚动条粘贴到具有相同高度的列表上
问题描述
这是我正在创建的文件管理系统代码的一部分。我试图将滚动条粘贴到列表框但失败了。我尝试使用注释行中的代码,它不起作用。另外,我在互联网上搜索了启用和禁用滚动条的代码。我没找到。我对 python 已经四个月大了。我会感谢你的帮助。
import tkinter as tk
from tkinter import ttk
from tkinter import font
class File_room(tk.Frame):
def __init__(self, master):
self.root = root
main_frame = ttk.Frame(self.root)
main_frame.pack(fill = "both", expand = True)
title_frame = ttk.Frame(main_frame, borderwidth = "5", width = 1350,
relief = tk.RIDGE)
title_frame.pack(side = tk.TOP)
self.title_lbl1 = tk.Label(title_frame, width = 30, font= ('georgia',
40, 'bold'), text= "\tFile Library Management System\t", padx = 12)
self.title_lbl1.grid()
data_frame = tk.Frame(main_frame, width = 1300, height = 400, relief=
tk.RIDGE)
data_frame.pack(side = tk.TOP)
data_frame_right = tk.LabelFrame(data_frame, font = ('georgia', 12,
'bold'), text = 'File Details', width = 400, height = 300, relief=
tk.RIDGE)
data_frame_right.pack(side = tk.RIGHT)
#********************************Right Widgets***********************
#--------------------------------Text Area---------------------------
self.file_list_lbl = tk.Label(data_frame_right, text = 'Country',
width = 6, font = ('georgia', 12, 'bold'))
self.file_list_lbl.grid(row = 0, column = 0, sticky = tk.W)
# self.file_list_lbl.pack(side = tk.LEFT)
self.list_display = tk.Listbox(data_frame_right, width = 32, height =
14, font = ('georgia', 12, 'bold'))
self.list_display.grid(row = 1, column = 2, sticky = tk.W)
# self.list_display.pack(side = tk.LEFT)
# self.txt_display.pack()
#Creating scroll bar for the file list
scroll_bar_file_list = ttk.Scrollbar(data_frame_right, orient =
'vertical', command=self.list_display.yview)
scroll_bar_file_list.grid(row = 1, column = 1, sticky = 'ns' )
# scroll_bar_file_list.pack(side = tk.RIGHT, fill = tk.Y)
self.list_display.config(yscrollcommand = scroll_bar_file_list.set)
# scroll_bar_file_list.pack(side = RIGHT, fill = Y)
#Creating file list
case_file_type = ['NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-', 'CE-',
'ID-', 'PK-', 'IA-', 'NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-',
'CE-', 'ID-', 'PK-', 'IA-', 'NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-',
'CE-', 'ID-', 'PK-', 'IA-']
file_list = tk.Listbox(data_frame_right, width = 5, height = 10, font
= ('georgia', 12, 'bold'),yscrollcommand =
scroll_bar_file_list.set)
# file_list.config(yscrollcommand = scroll_bar_file_list.set)
# file_list.bind('<<ListBoxSelect>>')
file_list.grid(row = 1 , column = 0, padx = 8)
# file_list.pack(side = tk.LEFT)
scroll_bar_file_list.config(command = file_list.yview)
for items in case_file_type:
file_list.insert(tk.END, items)
root = tk.Tk()
root.wm_title("File Library Management System")
root.config(background = 'limegreen')
app = File_room(root)
root.mainloop()
解决方案
您尝试将Scrollbar
第一个关联到self.list_display
,然后关联到file_list
。这是两个不同Listbox
的结果,导致了不良行为。
此外,要让您Listbox
的垂直大小匹配,请在列表框时Scrollbar
使用。sticky
grid
class File_room(tk.Frame):
def __init__(self, master):
...
#********************************Right Widgets***********************
#--------------------------------Text Area---------------------------
self.file_list_lbl = tk.Label(data_frame_right, text = 'Country',
width = 6, font = ('georgia', 12, 'bold'))
self.file_list_lbl.grid(row = 0, column = 0, sticky = tk.W)
self.list_display = tk.Listbox(data_frame_right, width = 32, height =
14, font = ('georgia', 12, 'bold'))
self.list_display.grid(row = 1, column = 2, sticky = tk.W)
scroll_bar_file_list = ttk.Scrollbar(data_frame_right, orient ='vertical')
scroll_bar_file_list.grid(row = 1, column = 1, sticky = 'ns' )
#Creating file list
case_file_type = ['NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-', 'CE-',
'ID-', 'PK-', 'IA-', 'NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-',
'CE-', 'ID-', 'PK-', 'IA-', 'NP-', 'CE-', 'ID-', 'PK-', 'IA-', 'NP-',
'CE-', 'ID-', 'PK-', 'IA-']
file_list = tk.Listbox(data_frame_right, width = 5, height = 10, font
= ('georgia', 12, 'bold'), yscrollcommand=scroll_bar_file_list.set)
file_list.grid(row = 1 , column = 0, padx = 8, sticky="ns") #use sticky here
scroll_bar_file_list.config(command = file_list.yview)
for items in case_file_type:
file_list.insert(tk.END, items)
tk.Button(data_frame_right, text="Click to delete", #add two buttons to test
command=lambda: file_list.delete(0)
).grid(row=1,column=3)
tk.Button(data_frame_right, text="Click to add",
command=lambda: file_list.insert(tk.END, "test")
).grid(row=1,column=4)
推荐阅读
- linux - 将多个文件从一个目录移动到远程 sftp 服务器上的另一个目录
- api - API 设计工具,例如 Swagger 用于 MQTT?
- java - Google Drive API Java 客户端库日志记录
- c - 为什么此功能不能与其他功能一起使用?
- javascript - 如何在另一个组件中使用 Slick.js 方法 - Vue
- c++ - 使用特征库向上和向左调整矩阵大小
- mamp - 在 mac osx 上更新到 Mamp 4.5 后出现 OCI 错误
- sql-server - T-SQL (Azure) 计算“单元格”的内容
- python - QWidgetAction 不显示菜单
- c# - 如何使用 Unity C# 检查蓝牙设备或 BLE 设备或信标的任何输出?