Guest_1013 2021-02-14 21:27 采纳率: 71.4%
浏览 739
已采纳

无端报tuple index out of range

​

# -*- coding: utf-8 -*-
import tkinter as tk
import time as tm
from PIL import ImageTk
import math as mt

hour_len = 50
minute_len = 75
sec_len = 100

root = tk.Tk()
root.title('PyTimer')
root.geometry('400x400')
root.attributes('-alpha',0.8)
root.configure(background='#002FA7')
root.minsize(400,400)
root.maxsize(400,400)
canvas = tk.Canvas(root,width=400,height=400,bg='#002FA7')
canvas.pack()
#bg = ImageTk.PhotoImage(file='sources\\bgglasses.jpg')
#canvas.create_image(0,0,image=bg)
canvas.create_oval(50,50,350,350)

def setTime():
    global entry
    entry = tk.Entry(root)
    entry.place(x=20,y=20)
    button_to_set = tk.Button(root,text='press me to set',command=helpSetTime)
    button_to_set.place(x=30,y=20)

def helpSetTime():
    global count_down
    count_down = entry.get()

def handler():
    global run
    run = False
root.protocol('WM_DELETE_WINDOW',handler)
run = True
button = tk.Button(root,text='test',command=setTime)
button.place(x=0,y=0)
while run:
    sec = tm.localtime(tm.time()).tm_sec
    minute = tm.localtime(tm.time()).tm_min
    hour = tm.localtime(tm.time()).tm_hour
    if sec < 10:
        sec_text = '0'+str(sec)
    else:
        sec_text = str(sec)
    if minute < 10:
        minute_text = '0' + str(minute)
    else:
        minute_text = str(minute)
    angle_sec = mt.pi*3/2-2*mt.pi*(sec/60)
    angle_minute = mt.pi*3/2-2*mt.pi*(minute/60)
    if minute >= 12:
        angle_hour = mt.pi*3/2-2*mt.pi*(hour-12)/12
    elif minute < 12:
        angle_hour = mt.pi*3/2-2*mt.pi*hour/12
    hour_text = str(hour)
    hour_hand = canvas.create_line(200,200,200-hour_len*mt.cos(angle_hour),200+hour_len*mt.sin(angle_hour))
    minute_hand = canvas.create_line(200,200,200-minute_len*mt.cos(angle_minute),200+minute_len*mt.sin(angle_minute))
    sec_hand = canvas.create_line(200,200,200-sec_len*mt.cos(angle_sec),200+sec_len*mt.sin(angle_sec))
    label = canvas.create_text(200,250,text=hour_text+':'+minute_text+':'+sec_text,font=('微软雅黑',15),activefill='#FFD058',disabledfill='#FFD058')
    canvas.update()
    global time_exist
    try:
        count_down
    except NameError:
        time_exist = False
    else:
        time_exist = True
    if time_exist:
        label_count_down = canvas.create_text(x=300,y=200,text=count_down)
        
    canvas.delete(hour_hand,minute_hand,sec_hand,label)
    print(time_exist)
    del(sec,minute,hour,angle_sec,angle_minute,angle_hour,hour_hand,minute_hand,sec_hand,label,hour_text,minute_text,sec_text)
    
root.destroy()

​

报错:

Traceback (most recent call last):
  File "C:\Users\12012\Desktop\PyTimer\main.pyw", line 74, in <module>
    label_count_down = canvas.create_text(x=300,y=200,text=count_down)
  File "C:\Users\12012\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2806, in create_text
    return self._create('text', args, kw)
  File "C:\Users\12012\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2767, in _create
    cnf = args[-1]
IndexError: tuple index out of range

请问是为什么?

  • 写回答

2条回答 默认 最新

  • dtsroy 2021-02-14 21:34
    关注

    把x=和y=去掉,这玩意不带关键字参数,直接写.

    亲测可用

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么
  • ¥15 Linux下安装搜狗拼音不能显示中文
  • ¥15 事件7000导致的自动重启如何解决
  • ¥15 (标签-matlab|关键词-m3)