将变量赋值给fuction调用时,dict不可调用

时间:2017-04-21 14:17:50

标签: python-3.x dictionary

我刚刚完成读取文本文件的函数并将其设置为字典,以便在简单的时钟应用程序中用作计划的一部分。函数计划在单独的脚本中可以正常工作并使用:< / p>

schedule = schedule()
print(schedule)

然而,当我在应用程序中以相同的方式调用它时,我得到一个'dict is not callable error',为什么我不能在我的应用程序中以相同的方式调用它?

#!/usr/bin/python3

# Full screen clock

import tkinter as tk
import time

def schedule():                 # this function
    flag = False
    schedule = dict()
    times = []
    days_of_the_week = ["Monday", "Tuesday", "Wednesday",
                        "Thursday", "Friday",
                        "Saturday", "Sunday"]
    with open("/home/pi/Desktop/schedule.txt", 'r') as f:
        for line in f:
            s = line.strip()
            if s == '':
                continue
            elif s in days_of_the_week:
                day = s
                times.clear()
            else:
                times.append(s)
                schedule[day] = times[:]

    return schedule

root = tk.Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
frame = tk.Frame(root)
frame.configure(background='black')
frame.pack(expand=tk.TRUE)
schedule = schedule()                 # function is called here


clock_lt = tk.Label(frame,
                 font=('Serene MTC', 230),
                 fg='#FF8000',
                 bg='black')
clock_lt.pack()

date_etc = tk.Label(frame,
                 font=('Ariel', 50),
                 fg='#FF8000',
                 bg='black')
date_etc.pack()

date_iso = tk.Label(frame,
                 font=('Ariel', 50),
                 fg='#FF8000',
                 bg='black')
date_iso.pack()

running_time = tk.Label(frame,
                        justify=tk.LEFT,
                 font=('Ariel', 15),
                 fg='#CDAF90',
                 bg='black')
running_time.pack()


def tick():
    schedule = schedule()
    time1 = ''
    time2 = time.strftime('%H:%M')
    date_etc_txt = "%s" % (time.strftime('%A')).upper()
    date_iso_txt = time.strftime('%d-%B-%Y').upper()
    today = date_etc_txt.title()


    if time2 != time1:
        time1 = time2
        clock_lt.config(text=time2)
        date_etc.config(text=date_etc_txt)
        date_iso.config(text=date_iso_txt)
        running_time.config(text=day_schedule[today])

    clock_lt.after(20, tick)

tick()
root.mainloop()

我不明白这里有什么不同?不幸的是,我不知道这段代码现在是否完全正常,因为我无法通过这个错误,但是如果你注释掉这一行:

schedule = schedule()

它工作正常。

以下是我正在使用的测试数据:

Friday
08:00 - 08:30   Morning briefing
09:15 - 10:30   Shakedown

Saturday
08:00 - 08:30   Morning briefing
09:00 - 10:00   Prep and Warm Up

Sunday 
08:00 - 08:30   Morning briefing
09:00 - 10:00   Prep and Warm Up

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您使用schedule作为变量和函数。

一旦你这样做,时间表不再是一个功能,而是一个变量。

结果解释器失去了它的思想,并告诉你你正试图调用一个dict(你认为它是一个函数),但你只是将它重新赋值为一个变量。

考虑使用不同的函数名称(可能是scheduler()

答案 1 :(得分:2)

schedule = schedule()

使用原始函数返回(在您的情况下为schedule)覆盖函数dict。从现在开始scheduledict(并且无法再被调用)。

将存储返回值的变量更改为其他值; e.g:

sched = schedule()

并确保正确使用scheddict)和schedule(函数)。