解决Python使用Tkinter的Notebook切换标签时出现的“Tk_GetPixmap: Error from CreateDIBSection 操作成功完成”闪退问题

零、问题描述

在使用Tkinter的Notebook控件时,对其标签进行切换,发现切换不了,一切换就报如下图错误:



第一个页面正常显示,后面的就都不行了,都是报这个错误。第一个页面里面是一些标签(label)、多选框(Checkbutton)、下拉框(Combobox)和按钮(Button),用的都是TTK的库,代码如下:

self.label_in_way = ttk.Label(self.tab_in_setting)
self.label_in_way.place(relx=0.015, rely=0.018, height=21, width=52)
self.label_in_way.configure(text='''输入方式''') self.cb_in_picture = ttk.Checkbutton(self.tab_in_setting)
self.cb_in_picture.place(relx=0.015, rely=0.073, relwidth=0.071, relheight=0.0, height=23)
self.cb_in_picture.configure(variable=self.cb_in_picture_var)
self.cb_in_picture.configure(text='''图片''') self.cb_in_video = ttk.Checkbutton(self.tab_in_setting)
self.cb_in_video.place(relx=0.106, rely=0.073, relwidth=0.071, relheight=0.0, height=23)
self.cb_in_video.configure(variable=self.cb_in_video_var)
self.cb_in_video.configure(text='''视频''') self.cb_in_net = ttk.Checkbutton(self.tab_in_setting)
self.cb_in_net.place(relx=0.197, rely=0.073, relwidth=0.071, relheight=0.0, height=23)
self.cb_in_net.configure(variable=self.cb_in_net_var)
self.cb_in_net.configure(text='''网络''') self.cb_in_camera = ttk.Checkbutton(self.tab_in_setting)
self.cb_in_camera.place(relx=0.288, rely=0.073, relwidth=0.089, relheight=0.0, height=23)
self.cb_in_camera.configure(variable=self.cb_in_camera_var)
self.cb_in_camera.configure(text='''摄像头''') self.cb_in_custom = ttk.Checkbutton(self.tab_in_setting)
self.cb_in_custom.place(relx=0.394, rely=0.073, relwidth=0.089, relheight=0.0, height=23)
self.cb_in_custom.configure(variable=self.cb_in_custom_var)
self.cb_in_custom.configure(text='''自定义''') self.label_in_source = ttk.Label(self.tab_in_setting)
self.label_in_source.place(relx=0.015, rely=0.145, height=21, width=52)
self.label_in_source.configure(text='''输入来源''') self.cb_in_source = ttk.Combobox(self.tab_in_setting)
self.cb_in_source.place(relx=0.015, rely=0.2, relheight=0.042, relwidth=0.964)
self.cb_in_source.configure(textvariable=self.cb_in_source_var) self.btn_in_source_choose = ttk.Button(self.tab_in_setting)
self.btn_in_source_choose.place(relx=0.848, rely=0.255, height=27, width=87)
self.btn_in_source_choose.configure(text='''选择''')

第二个页面页差不多是这些东西,多选框换成了输入框(Entry)而已,代码如下:

self.label_save_type = ttk.Label(self.tab_out_setting)
self.label_save_type.place(relx=10.0, rely=10.0, height=21, width=52)
self.label_save_type.configure(text='''保存类型''') self.cb_prediction_info = ttk.Checkbutton(self.tab_out_setting)
self.cb_prediction_info.place(relx=10.0, rely=40.0, relwidth=71.0, relheight=0.0, height=23)
self.cb_prediction_info.configure(variable=self.cb_prediction_info_var)
self.cb_prediction_info.configure(text='''预测信息''') self.cb_prediction_video = ttk.Checkbutton(self.tab_out_setting)
self.cb_prediction_video.place(relx=100.0, rely=40.0, relwidth=71.0, relheight=0.0, height=23)
self.cb_prediction_video.configure(variable=self.cb_prediction_video_var)
self.cb_prediction_video.configure(text='''预测视频''') self.cb_raw_video = ttk.Checkbutton(self.tab_out_setting)
self.cb_raw_video.place(relx=200.0, rely=40.0, relwidth=71.0, relheight=0.0, height=23)
self.cb_raw_video.configure(variable=self.cb_raw_video_var)
self.cb_raw_video.configure(text='''原始视频''') self.label_save_path = ttk.Label(self.tab_out_setting)
self.label_save_path.place(relx=10.0, rely=80.0, height=21, width=52)
self.label_save_path.configure(text='''保存位置''') self.entry_path = ttk.Entry(self.tab_out_setting)
self.entry_path.place(relx=10.0, rely=110.0, relheight=23.0, relwidth=636.0) self.btn_choose_save_path = ttk.Button(self.tab_out_setting)
self.btn_choose_save_path.place(relx=560.0, rely=140.0, height=27, width=87)
self.btn_choose_save_path.configure(text='''选择''')



到某度上查不到啥资料,只能自己解决,折腾了半天,碰巧发现了解决方案。

壹、解决问题

我做了如下尝试:

  1. 把页面二的所有控件清空,切换到页面二,不报错
  2. 页面二只留下标签(Label),不报错,也不显示
  3. 页面二只留下多选框(Checkbutton),也不报错,不显示
  4. 页面二只留下输入框(Entry),报错
  5. 页面二只留下按钮(Button),不报错,也不显示
  6. 页面二只删除输入框(Entry),不报错,也不显示

得出是输入框造成报错的结论,但是不显示是怎么回事呢?不知道

第一次尝试

简单分析页面一和页面二的情况,其他控件都一样,就输入框(Entry)和下拉框(Combobox)不一样,其中,下拉框设置了textvariable属性,输入框则没有,而下拉框是可以正常使用的,所以怀疑是textvariable属性的问题。

给输入框加上textvariable属性:

self.entry_path_var = tk.StringVar()  # 创建值
self.entry_path = ttk.Entry(self.tab_out_setting)
self.entry_path.place(relx=10.0, rely=110.0, relheight=23.0, relwidth=636.0)
self.entry_path.configure(textvariable=self.entry_path_var) # 设置值

还是报错。

第二次尝试

在刚刚的尝试中有一个现象,不报错也不显示,有没有一种可能,它不是不显示,只是显示到屏幕外边去了?

这边给它设置位置的就是place函数了,简单查了下它的用法,其中比较重要的:

relx=amount - locate anchor of this widget between 0.0 and 1.0 relative to width of master (1.0 is right edge)

relx的作用是指定相对坐标,relx的取值为0~1的小数。如果relx=0,表示子控件的x方向的起始位置在父控件的最左边;如果rely=1,表示子控件的y方向的起始位置在父控件的最右边。

然后看了下我的代码,relxrelyrelheightrelwidth全都大于1了,可能是这个的问题。

重新改代码如下(尝试了一个控件,通过了,然后把一页的改了):

self.label_save_type = ttk.Label(self.tab_out_setting)
self.label_save_type.place(relx=0.015, rely=0.018, height=21, width=52)
self.label_save_type.configure(text='''保存类型''') self.cb_prediction_info = ttk.Checkbutton(self.tab_out_setting)
self.cb_prediction_info.place(relx=0.015, rely=0.073, relwidth=0.108, relheight=0.0, height=23)
self.cb_prediction_info.configure(variable=self.cb_prediction_info_var)
self.cb_prediction_info.configure(text='''预测信息''') self.cb_prediction_video = ttk.Checkbutton(self.tab_out_setting)
self.cb_prediction_video.place(relx=0.152, rely=0.073, relwidth=0.108, relheight=0.0, height=23)
self.cb_prediction_video.configure(variable=self.cb_prediction_video_var)
self.cb_prediction_video.configure(text='''预测视频''') self.cb_raw_video = ttk.Checkbutton(self.tab_out_setting)
self.cb_raw_video.place(relx=0.303, rely=0.073, relwidth=0.108, relheight=0.0, height=23)
self.cb_raw_video.configure(variable=self.cb_raw_video_var)
self.cb_raw_video.configure(text='''原始视频''') self.label_save_path = ttk.Label(self.tab_out_setting)
self.label_save_path.place(relx=0.015, rely=0.145, height=21, width=52)
self.label_save_path.configure(text='''保存位置''') self.entry_path = ttk.Entry(self.tab_out_setting)
self.entry_path.place(relx=0.015, rely=0.2, relheight=0.042, relwidth=0.964) self.btn_choose_save_path = ttk.Button(self.tab_out_setting)
self.btn_choose_save_path.place(relx=0.848, rely=0.255, height=27, width=87)
self.btn_choose_save_path.configure(text='''选择''')

一切正常!

贰、总结

至此,问题解决,是调用place函数时传入的参数不正确造成的,rel(relative)开头的参数一般传入值的区间应该是[0,1],改过就好了。我的代码是使用Page 7.6生成的,故没有多管它调用每个函数的细节。本次错误应该算是page 7.6的一个Bug。

叁、参考文献

  1. 记录一个没解决的createDIBSection失败的bug
  2. CreateDIBSection函数详解
  3. tkinter-place布局详解
  4. Python之tkinter 多选项卡 Notebook

【Python】解决“Tk_GetPixmap: Error from CreateDIBSection”闪退问题的更多相关文章

  1. 解决Xamarin Android SDK Manager闪退问题

    解决Xamarin Android SDK Manager闪退问题 SDK Manager闪退是因为它找不到java.exe导致的.SDK Manager默认是通过读取注册表中JDK安装信息来java ...

  2. 【已解决】极速迅雷win10闪退解决方案

    [已解决]极速迅雷win10闪退解决方案 本文作者:天析 作者邮箱:2200475850@qq.com 发布时间: Wed, 17 Jul 2019 18:01:00 +0800 在吾爱下载了个极速迅 ...

  3. 解决Mysql命令行输入密码闪退问题

    输入密码闪退是因为后台Mysql服务没有启动. 解决办法:我的电脑,右键管理,服务,查看服务里面Mysql是否在运行.如果没有在运行那么可以右键启动,最好属性中设置为自动启动.

  4. 解决关于ios访问相机闪退问题

    在mac上的ionic3项目打包成苹果app,系统版本是10.3.3 . 当调用相机的时候出现闪退情况,这是调试出现的问题: This app has crashed because it attem ...

  5. reedis 解决在windows下启动闪退

    windows下安装https://github.com/MicrosoftArchive/redis/releases第一次启动报错: [2368] 21 Apr 02:57:05.611 # Cr ...

  6. 解决ubuntu16.04软件中心闪退的问题

    依次执行下列命令 sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install --reinstall software-cen ...

  7. 解决anaconda3打开不了闪退

    今天想新创个环境,结果发现创不起,而且anaconda居然也进不去了. 然后尝试了网上各种方法,修改c:user/用户/用户名目录下的.condarc文件,镜像源,包括重装都没用. 最后 把.cond ...

  8. 安装两个版本的python安装包,后安装的python程序打开时闪退

    1.环境变量的问题 (Win7)右键打开“计算机”的属性设置→高级系统设置→环境变量.  在系统变量中的path中,编辑,在末尾加入Python的安装路径“F:\Python27”, 路径与路径之间使 ...

  9. iOS 启动连续闪退保护方案

    引言 “如果某个实体表现出以下任何一种特性,它就具备自主性:自我修复.自我保护.自我维护.对目标的自我控制.自我改进.” —— 凯文·凯利 iOS App 有时可能遇到启动必 crash 的绝境:每次 ...

  10. Activity Monitor 闪退 & 无法进入睡眠

    情况描述 黑苹果主机突然无法进入睡眠. 考虑到可能是后台程序阻碍了系统正常进入睡眠, 于是想要通过Activity Monitor查看系统的活动情况,然而,Activity Monitor闪退. 重 ...

随机推荐

  1. 中间件 vs 过滤器

    在 ASP.NET Core 中,中间件(Middleware) 和 过滤器(Filters) 是两种不同的机制,用于处理请求和响应的管道.它们的作用和适用场景有所不同,以下是它们的对比和详细说明: ...

  2. 【源码】Kafka订制协议如何处理粘拆包

    前言 在上一篇随笔中,我们探讨了如何使用 Netty 处理自定义协议中的粘包和拆包问题.Netty 提供了高度封装的 API,帮助开发者轻松应对这一挑战,因此很多人都对其解决方案非常熟悉. 但如果我们 ...

  3. w3cschool-OpenResty 最佳实践

    https://www.w3cschool.cn/openresty1/ OpenResty 简介 OpenResty(也称为 ngx_openresty)是一个全功能的 Web 应用服务器.它打包了 ...

  4. biancheng-Linux教程

    目录http://c.biancheng.net/linux_tutorial/ 1Linux简介2Linux安装3Linux文件和目录管理4Linux打包(归档)和压缩5Vim文本编辑器6Linux ...

  5. Eclipse中的快捷键:批量修改指定的变量名、方法名、类名等:alt + shift + r

    /* * Eclipse中的快捷键: * 1.补全代码的声明:alt + / * 2.快速修复: ctrl + 1 * 3.批量导包:ctrl + shift + o * 4.使用单行注释:ctrl ...

  6. ORACLE 中报ORA-30926 无法在源表中获得稳定的行的处理

    在库存处理的业务中有这么一个场景,一张处方划价单进行库存扣减处理,如果此单据同一商品有两行以上,同时扣减同一行库存记录,使用MERGE INTO批量更新是就会报错:ORA-30926 无法在源表中获得 ...

  7. dart子类的继承

    1 Dart中类的继承 1.子类使用extends关键字来继承父类 2.子类会继承父类里面可见的属性和方法,但是不会继承构造函数 3. 子类能够复写父类的方法 getter和setter方法 2Dar ...

  8. 首批!天翼云率先通过ITU国际标准认证!

    近日,天翼云通过国内唯一人工智能云平台领域的ITU国际标准评估--中国信通院组织的ITU-T F.AICP-GA人工智能云平台技术规范国际标准和<智算工程平台能力要求>国内标准一致性评估, ...

  9. 单用户模式启动 CentOS/RHEL 7/8 的三种方法

    单用户模式启动 CentOS/RHEL 7/8 的三种方法   单用户模式,也被称为维护模式,超级用户可以在此模式下恢复/修复系统问题. 通常情况下,这类问题在多用户环境中修复不了.系统可以启动但功能 ...

  10. Luogu P1220 关路灯 题解 [ 蓝 ][ 区间dp ]

    原题 关路灯 题目描述 某一村庄在一条路线上安装了 \(n\) 盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏 ...