我剛完成讀取文本文件並將其設置爲字典的函數,以作爲簡單時鐘應用程序中的一部分調度使用,函數調度可以很好地工作自己在一個單獨的腳本,並使用名爲:當把變量賦值給函數時,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
什麼我做錯了嗎?
所以我改名爲變量名day_schedule,我得到了相同的結果回溯(最後最近一次調用)? ./fsclock.py「,第65行,格式爲 day_schedule = schedule() TypeError:'dict'對象不可調用 – iFunction
您是在IDLE中運行它嗎(還是另外一個IDE,它可以運行python解釋器?)。在這種情況下,你需要重新啓動解釋器。 –
和'schedule = schedule()'行會在您的代碼中出現兩次。第二次在'tick()'函數中。 –