Guest_1013
2021-02-14 21:27
采纳率: 87.5%
浏览 217

无端报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条回答 默认 最新

  • DogMonkeys 2021-02-14 21:34
    已采纳

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

    亲测可用

    点赞 1 评论
  • DogMonkeys 2021-02-14 21:34

     

    点赞 1 评论

相关推荐 更多相似问题