tkinter学习笔记

这篇博客探讨了在tkinter中进行debug的方法,遇到的Tkinter.TclError: bad text index "add"问题,以及如何正确添加jpg图片。同时,提到了pickle模块在程序中的可能应用,并对比了两种不同的编程组织方式:按照安装顺序和按照结构顺序编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tkinter

tkinter 怎么做debug?

def insert_end():
    var = e.get()
    t.insert('add', var)

File “D:\torchenv\lib\tkinter_init_.py”, line 3738, in insert
self.tk.call((self._w, ‘insert’, index, chars) + args)
_tkinter.TclError: bad text index “add”

def insert_end():
    var = e.get()
    t.insert('end', var)

2

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\torchenv\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "E:/Practice/python_work/Jupyter/Tkinter/tkinter_pycharm/selection_list.py", line 17, in print_selection
    value = lb.get(lb.curselection())
  File "D:\torchenv\lib\tkinter\__init__.py", line 3182, in get
    return self.tk.call(self._w, 'get', first)
_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number

这是因为没有选择任何一项

1

def usr_login():
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    try:
        with open('usrs_info.pickle', 'rb') as usr_file:
            usr_info = pickle.load(usr_file)
    except FileExistsError: #这里错了
        with open('usrs_info.pickle', 'ab+') as usr_file:
            usr_info = {'admin', 'admin'}
            pickle.dump(usr_info, usr_file)

Exception in Tkinter callback Traceback (most recent call last): File "D:\torchenv\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "E:/Practice/python_work/Jupyter/Tkinter/tkinter_pycharm/No13_WelcomeWindow.py", line 36, in usr_login with open('usrs_info.pickle', 'rb') as usr_file: FileNotFoundError: [Errno 2] No such file or directory: 'usrs_info.pickle'
except处理的问题还是显示出来了,说明不对

def usr_login():
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    try:
        with open('usrs_info.pickle', 'rb') as usr_file:
            usr_info = pickle.load(usr_file)
    except FileNotFoundError:
        with open('usrs_info.pickle', 'ab+') as usr_file:
            usr_info = {'admin', 'admin'}
            pickle.dump(usr_info, usr_file)

2

    except FileNotFoundError:
        with open('usrs_info.pickle', 'wb') as usr_file:
            usr_info = {'admin', 'admin'}
            pickle.dump(usr_info, usr_file)

debug

type(usr_info)
<class 'set'>

Exception in Tkinter callback Traceback (most recent call last): File "D:\torchenv\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "E:/Practice/python_work/Jupyter/Tkinter/tkinter_pycharm/No13_WelcomeWindow.py", line 45, in usr_login if usr_pwd == usr_info[usr_name]: TypeError: 'set' object is not subscriptable

    except FileNotFoundError:
        with open('usrs_info.pickle', 'wb') as usr_file:
            usr_info = {'admin': 'admin'}  #逗号改为了冒号
            pickle.dump(usr_info, usr_file)
type(usr_file)
<class '_io.BufferedRandom'>
type(usr_info)
<class 'dict'>

3

tkmsgbox.showinfo(title='Welcome', text='You are in circle')

Exception in Tkinter callback Traceback (most recent call last): File "D:\torchenv\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "E:/Practice/python_work/Jupyter/Tkinter/tkinter_pycharm/No13_WelcomeWindow.py", line 73, in sign_to_python tkmsgbox.showinfo(title='Welcome', text='You are in circle') File "D:\torchenv\lib\tkinter\messagebox.py", line 84, in showinfo return _show(title, message, INFO, OK, **options) File "D:\torchenv\lib\tkinter\messagebox.py", line 72, in _show res = Message(**options).show() File "D:\torchenv\lib\tkinter\commondialog.py", line 44, in show s = w.tk.call(self.command, *w._options(self.options)) _tkinter.TclError: bad option "-text": must be -default, -detail, -icon, -message, -parent, -title, or -type

tkmsgbox.showinfo(title='Welcome', message='You are in circle')

问题

  1. tk 如何添加jpg?
  2. pickle文件?
  3. pickle?
label_1 = tk.Label(window, bg='yellow', width=30, height=2, text='empty').pack()

Exception in Tkinter callback Traceback (most recent call last): File "D:\torchenv\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "E:/Practice/python_work/Jupyter/Tkinter/tkinter_pycharm/Radiobutton.py", line 14, in print_selection label_1.config(text='you have selected' + selected_var.get()) AttributeError: 'NoneType' object has no attribute 'config'

label_1 = tk.Label(window, bg='yellow', width=30, height=2, text='empty')
label_1.pack()
    if (var1 == 1) & (var2 == 0):
        label_1.config(text='Only Python')

我是按照“安装”顺序从上到下写程序的

莫烦是按照结构 从上到下写程序的,写了结构以后,再回过头往结构里面填充内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值