解决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. 老奶奶看了都会的WSL2连接USB设备教程!

    老奶奶看了都会的WSL2-Ubuntu连接USB设备教程! 作者:SkyXZ CSDN:SkyXZ--CSDN博客 博客园:SkyXZ - 博客园 参考资料:微软官方文档连接 USB 设备 | Mic ...

  2. 【刷题】牛客模拟面试 > 模拟面试报告

    https://www.nowcoder.com/interview/ai/index 1-TCP协议的流量控制和拥塞控制 TCP的流量控制是基于窗口机制实现的: 在建立连接时, 发送方和接收方都会建 ...

  3. java线程用法和区别

    从操作系统的角度讲,os会维护一个ready queue(就绪的线程队列).并且在某一时刻cpu只为ready queue中位于队列头部的线程服务.但是当前正在被服务的线程可能觉得cpu的服务质量不够 ...

  4. w3cschool-Netty 实战精髓篇2

    https://www.w3cschool.cn/essential_netty_in_action/essential_netty_in_action-adkv28bm.html 研究Netty中的 ...

  5. 利用JavaScript自定义事件完成组件间的数据通信

    我们知道,在JavaScript中,原生DOM事件在开发中是很有用的(与用户交互的重要方式),但是操作原生DOM事件其实有两大缺点:性能低.依赖于浏览器(NodeJs.小程序等不可用).那么这个时候, ...

  6. 创建多线程的方式二:实现Runnable接口

      /** * 创建多线程的方式二:实现Runnable接口 * 1. 创建一个实现了Runnable接口的类 * 2. 实现类去实现Runnable中的抽象方法:run() * 3. 创建实现类的对 ...

  7. Jordak pg walkthrough Intermediate

    NMAP ┌──(root㉿kali)-[/home/ftpuserr] └─# nmap -p- -A 192.168.226.109 Starting Nmap 7.94SVN ( https:/ ...

  8. moectf2023 web wp

    gas!gas!gas! 直接跑脚本 import requests session=requests.Session() url="http://127.0.0.1:14447" ...

  9. RAW镜像格式介绍

    本文分享自天翼云开发者社区<RAW镜像格式介绍>,作者:z****n RAW(Raw Disk Image)是一种简单而基本的虚拟化镜像格式,用于存储虚拟机的磁盘内容.它是一种原始的二进制 ...

  10. 利用Python开发Exporter,集成Prometheus和Grafana对进程监控

    利用Python开发Exporter,集成Prometheus和Grafana对进程监控 在现代软件开发和运维中,监控是确保系统稳定运行和快速响应问题的重要手段.Prometheus和Grafana的 ...