首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何实现Python tkinter中三个Listbox的同步滚动条?

如何实现Python tkinter中三个Listbox的同步滚动条?

提问于 2024-08-28 03:02:58
回答 0关注 0查看 82

在Python的tkinter库中,为三个Listbox控件分别配置独立的Scrollbar控件,同时确保当一个Listbox滚动时,其他两个Listbox能够同步滚动。

现在我主要的问题就是用鼠标滚动一个Listbox时滚动速度总会快于其他两个。还有就是拉动Scrollbar控件时无法同步滚动。(更进一步:实现一个开关控制是否开启同步滚动)

下面是我的代码:

代码语言:python
运行
AI代码解释
复制
import tkinter as tk
from tkinter import ttk

def on_scroll(event, *listboxes):
    print(event, event.delta)
    # 根据滚动事件获取滚动条的滚动位置
    x = event.x
    y = event.y
    if event.num == 5 or event.delta > 0:
        # 向上滚动或鼠标滚轮向上滚动
        y = -1
    elif event.num == 4 or event.delta < 0:
        # 向下滚动或鼠标滚轮向下滚动
        y = 1
    # # 更新所有Listbox的滚动位置
    for lbox in listboxes:
        lbox.yview_scroll(int(-1*(event.delta/120)), "units")

def create_listbox_with_scrollbar(master, listbox, scrollbar):
    # 将滚动条的command设置为更新Listbox的滚动位置
    scrollbar.config(command=lambda *args: listbox.yview(*args))
    # 将Listbox的滚动命令设置为滚动条的滚动函数
    listbox.config(yscrollcommand=lambda *args: scrollbar.set(*args))
    # 将Listbox和滚动条添加到主窗口
    listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    scrollbar.pack(side=tk.LEFT, fill=tk.Y)

def create_app():
    root = tk.Tk()
    root.title("同步滚动的Listbox")

    # 创建三个Listbox和它们的滚动条
    listbox1 = tk.Listbox(root, width=20, height=10)
    scrollbar1 = ttk.Scrollbar(root, orient="vertical", command=listbox1.yview)

    listbox2 = tk.Listbox(root, width=20, height=10)
    scrollbar2 = ttk.Scrollbar(root, orient="vertical", command=listbox2.yview)

    listbox3 = tk.Listbox(root, width=20, height=10)
    scrollbar3 = ttk.Scrollbar(root, orient="vertical", command=listbox3.yview)

    # 将Listbox和滚动条添加到主窗口
    create_listbox_with_scrollbar(root, listbox1, scrollbar1)
    create_listbox_with_scrollbar(root, listbox2, scrollbar2)
    create_listbox_with_scrollbar(root, listbox3, scrollbar3)

    # 绑定滚动事件到所有Listbox
    for lbox in [listbox1, listbox2, listbox3]:
        lbox.bind("<MouseWheel>", lambda event: on_scroll(event, listbox1, listbox2, listbox3))

    # 填充一些示例数据到Listbox
    for i in range(100):
        listbox1.insert(tk.END, f"Item 1 {i}")
        listbox2.insert(tk.END, f"Item 2 {i}")
        listbox3.insert(tk.END, f"Item 3 {i}")

    root.mainloop()

create_app()

感谢!!!

回答 2

jwj

修改于 2021-09-14 03:01:46

可以提供下仓库地址吗?

用户1057325

修改于 2021-09-15 06:35:45

帮顶

和开发者交流更多问题细节吧,去 写回答
相关文章
解决 json.dump 报错:TypeError - Object of type xxx is not JSON serializable
在python中导入json包可以方便地操作json文件,但是偶尔会遇到 TypeError: Object of type xxx is not JSON serializable 错误,通常报错的位置是很正常的int或float,本文记录该问题解决方法。 自定义序列化方法 class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer):
为为为什么
2022/08/05
2.4K0
关于TypeError: Object of type 'datetime' is not JSON serializable 解决方法
python中这个错误的原因是json.dumps无法对字典中的datetime时间格式数据进行转化,dumps的原功能是将dict转化为str格式,不支持转化时间,所以需要将json类部分内容重新改写,来处理这种特殊日期格式。
用户7886150
2021/01/17
4.1K0
关于json.dumps的使用和解决Object of type XXX is not JSON serializable错误
JSON是一种轻量级的数据交换格式。采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
python与大数据分析
2022/03/11
2.8K0
关于json.dumps的使用和解决Object of type XXX is not JSON serializable错误
【C#】Newtonsoft.Json 中 JArray 添加数组报错:Could not determine JSON object type for type 'xxx'
有时我们临时需要一个 JSON 字符串,直接拼接肯定不是好方法,但又懒得去定义一个类,这是用 JObject 就会非常的方便。
丹枫无迹
2020/04/08
1.7K0
python __proxy__ is not JSON serializable
详细错误如下 TypeError at / Object of type __proxy__ is not JSON serializable Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.2.2 Exception Type: TypeError Exception Value: Object of type __proxy__ is not JSON serializable Except
小贝壳
2020/03/05
1.3K0
R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettable
object of type ‘symbol‘ is not subsettable
拓端
2020/10/21
1.9K0
Python报错:TypeError: the JSON object must be str, bytes or bytearray, not ‘dict‘
当我尝试运行以下代码,来练习使用json.loads()和json.dumps()函数时,系统给我报出了这个错误。
JavaEdge
2021/02/23
3.2K0
org.apache.flink.table.api.TableException: A raw type backed by type information has no serializable
出现这个问题是因为Order类是嵌套内部类,由于不是public类型而导致出现了下面的异常:
johnhuster的分享
2022/03/28
8920
Expected object of scalar type Float but got scalar type Double for argument
在pytorch中float32为float类型,而float64则为double类型,注意tensor的数据类型。
狼啸风云
2020/12/01
1.8K0
Expected object of scalar type Float but got scalar type Double for argument
Typescript: Access window object with type check
We have an external html provide window.configs.
szhshp
2022/09/21
2070
Pandas 创建DataFrame提示:type object ‘object‘ has no attribute ‘dtype‘
data为空,且dtype默认为空时 出现type object ‘object’ has no attribute ‘dtype’告警
全栈程序员站长
2022/08/26
1.6K0
Pandas 创建DataFrame提示:type object ‘object‘ has no attribute ‘dtype‘
datetime.date(2014, 4, 25) is not JSON serializable
# 背景 接口期望返回json格式数据,但数据存储在mysql中,先将mysql的数据转为dict,然后将dict转为json格式,然后就报这个错误了的,原因就是时间格式转换问题 # 解决方法 1. 创建这么一个方法 def date_handler(obj): if hasattr(obj, 'isoformat'): return obj.isoformat() else: raise TypeError 2. 然后这样转换: json.dumps(peo
千往
2018/04/11
6170
python中的type和object详解
这篇主要描述Python的新风格对象(new-style objects),如下:
用户7886150
2021/01/27
8390
Postman报错Unsupported Media Type
Media Type,即是Internet Media Type,互联网媒体类型,也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。 例如: Content-Type: text/html;charset:utf-8; 常见的媒体格式类型如下:
全栈程序员站长
2022/08/24
1.1K0
Postman报错Unsupported Media Type
python中的type和object详解
这篇博客主要描述Python的新风格对象(new-style objects),如下:
全栈程序员站长
2022/09/07
5830
python中的type和object详解
PHP“Cannot use object of type stdClass as array”
 php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误
Java架构师必看
2021/03/22
1.1K0
SQL函数 JSON_OBJECT
JSON_OBJECT接受逗号分隔的键:值对列表(例如,‘MyKey’:colname),并返回包含这些值的JSON对象。可以指定任何单引号字符串作为键名;JSON_OBJECT不强制任何命名约定或对键名进行唯一性检查。可以为值指定列名或其他表达式。
用户7741497
2022/04/06
3.1K0
MySQL Decimal is not JSON serializable以及插入小数变成0
使用Python搭建的web服务,后台读取MySQL数据后,需要将数据序列化为json串,返回给前端。但是如果MySQL的字段是decimal类型,序列化为json串就会遇到麻烦。会报如下错误 raise TypeError(repr(o) + " is not JSON serializable") TypeError: Decimal('0') is not JSON serializable HTTP/1.0" 500 网上有一些解决方案,但是如果你对于数据精度的要求没那么高的话,完全可以把MySQL中的decimal字段的类型改为float,float类型是可以直接进行json序列化的。这样只修改数据库,不修改代码,就可以修复问题。参考下图。另外,设置float类型的时候,小数点后一定要设置,可以设置为4,表示带4位小数。否则默认可能是带0位小数,就不准确了。如果你insert的数据类似‘0.022’这种,在数据库中就变成0了。
震八方紫面昆仑侠
2020/07/23
2.8K0
MySQL Decimal is not JSON serializable以及插入小数变成0
Convert an object into Json using SBJson or other JSON library
Using SBJson, to convert a object to JSON string, you have to override the proxyForJson method. Like the following, The .h file, @interface MyCustomObject : NSObject {     NSString *receiverFirstName;     NSString *receiverMiddleInitial;     NSString *rece
阿新
2018/04/12
6470
点击加载更多

相似问题

OCR报错,check content-type and body?

0208

'int' object is not subscriptable 报错 要怎么改?

1834

VBA请求文字报错check content-type and body?

0331

报错'NoneType' object has no attribute 'find_all'?

01.9K

scf报错执行函数后报错?

1310
相关问答用户
腾讯云TDP | 产品KOL擅长5个领域
新浪微博 | 高级总监擅长4个领域
某公司 | 程序员擅长1个领域
擅长5个领域
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档