2017-04-21 95 views
0

我剛完成讀取文本文件並將其設置爲字典的函數,以作爲簡單時鐘應用程序中的一部分調度使用,函數調度可以很好地工作自己在一個單獨的腳本,並使用名爲:當把變量賦值給函數時,dict不可調用

schedule = schedule() 
print(schedule) 

然而,當我把它在應用程序中以同樣的方式,我得到一個「字典是不可調用的錯誤」,爲什麼我不能把它在我的應用程序中的相同方式

#!/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

您正在使用schedule作爲變量和函數。

一旦你這樣做,時間表不再是一個函數,而是一個變量。

因此,解釋器失去了它的思想,並告訴你,你正試圖調用一個字典(你認爲是一個函數),但你只是將它重新分配爲一個變量。

考慮使用不同的函數名(也許scheduler()

2
schedule = schedule() 

覆蓋功能schedule與任何原始功能returnes(在你的情況下,dict)。從現在起scheduledict(並且不能再被調用)。

更改您將返回值存儲到其他位置的變量;例如: -

sched = schedule() 

,並確保使用sched(這是一個dict)和schedule(功能)正確。在 打勾() 文件「 文件「./fsclock.py」,行82:

+0

所以我改名爲變量名day_schedule,我得到了相同的結果回溯(最後最近一次調用)? ./fsclock.py「,第65行,格式爲 day_schedule = schedule() TypeError:'dict'對象不可調用 – iFunction

+0

您是在IDLE中運行它嗎(還是另外一個IDE,它可以運行python解釋器?)。在這種情況下,你需要重新啓動解釋器。 –

+1

和'schedule = schedule()'行會在您的代碼中出現兩次。第二次在'tick()'函數中。 –