yzc-jpj 2026-01-21 17:10 采纳率: 50%
浏览 7

python输入内容报错

大家好,我是一位初中生。我在空闲时做了一个python小项目练手,但遇到了一个报错,程序总是无法识别输入的最后一个名称

报错如下:


```python
Exception in Tkinter callback
Traceback (most recent call last):      
  File "c:\Users\yzc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1916, in __call__
    return self.func(*args)
  File "D:\python\newapp\ui.py", line 13, in fuck
    label2.config(name)
  File "c:\Users\yzc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1670, in configure
    return self._configure('configure', cnf, kw)
  File "c:\Users\yzc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1659, in _configure
    return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
_.py", line 1647, in _getconfigure1
    x = self.tk.splitlist(self.tk.call(*args))     
_tkinter.TclError: unknown option "-色彩"

代码如下

```python
import tkinter as tk
import random 
import time
from tkinter import ttk
infomation=''
text2=[]

def fuck():
    global text2,infomation
    text2=text.get('1.0','end-1c').split()
    name=random.choice(text2)
    infomation=name
    label2.config(name)

win=tk.Tk()
win.geometry('300x400+100+100')
win.title('随机抽取学生')

label=tk.Label(win,text='学生抽取器',font='12')
label.pack(padx=5,pady=5)
notebook=ttk.Notebook(win)
notebook.pack()
RandomFrame=tk.Frame(notebook)
CheckFrame=tk.Frame(notebook)
OkFrame=tk.Frame(notebook)

notebook.add(RandomFrame,text='输入')
notebook.add(OkFrame,text='交付')

text=tk.Text(RandomFrame)
text.pack()
label1=tk.Label(OkFrame,text='本期幸运儿是')
label1.pack()
label2=tk.Label(OkFrame,text=infomation)
label2.pack()
button=tk.Button(OkFrame,text='开始',command=fuck)
button.pack(padx=10,pady=10)

win.mainloop()

望告知解决办法,谢谢

  • 写回答

3条回答 默认 最新

  • a5156520 2026-01-21 20:43
    关注

    根据代码来看,应该是全局变量text2需要初始化一下,然后第16行设置标签属性需要修改下。

    修改如下:

    参考链接:


    import tkinter as tk
    import random 
    import time
    from tkinter import ttk
    infomation=''
    
    # https://blog.csdn.net/simcode/article/details/155270389
    # 根据代码来看,这里的全局变量需要设置为字典,这里为了测试方便,简单设置了一下
    # 将需要点名的学生姓名放到第二个字符串里面,即下面的'张三 李四 王五'里面
    # 使用空格分隔每个姓名字符串
    text2={
        '1.0':'张三 李四 王五'
    
        }
     
    def fuck():
        #print("值:", t.get("1.0"))
        global text2,infomation
        t2=text2.get('1.0','end-1c').split()
        #print("text2=", text2)
        name=random.choice(t2)
        infomation=name
        # https://blog.51cto.com/u_16213713/12378270
        # 设置标签属性这里修改下
        label2.config(text=name)
     
    win=tk.Tk()
    win.geometry('300x400+100+100')
    win.title('随机抽取学生')
     
    label=tk.Label(win,text='学生抽取器',font='12')
    label.pack(padx=5,pady=5)
    notebook=ttk.Notebook(win)
    notebook.pack()
    RandomFrame=tk.Frame(notebook)
    CheckFrame=tk.Frame(notebook)
    OkFrame=tk.Frame(notebook)
     
    notebook.add(RandomFrame,text='输入')
    notebook.add(OkFrame,text='交付')
     
    text=tk.Text(RandomFrame)
    text.pack()
    label1=tk.Label(OkFrame,text='本期幸运儿是')
    label1.pack()
    label2=tk.Label(OkFrame,text=infomation)
    label2.pack()
    button=tk.Button(OkFrame,text='开始',command=fuck)
    button.pack(padx=10,pady=10)
     
    win.mainloop()
     
    
    

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 1月21日