所以我试图每次用户点击一个按钮时添加7天到日期。 这里是我的代码的简化版本:使用按钮更新变量
import sys
if sys.version_info[0] == 2: # Just checking your Python version to import Tkinter properly.
import Tkinter as tk
import ttk as ttk
else:
import tkinter as tk
from tkinter.ttk import ttk as ttk
import datetime
import calendar
def nextweek(cd, ow):
newdate=(cd+ow)
cd=newdate
return cd
def printdate(cd):
print cd
curdate = datetime.date(2016, 01, 04)
one_week = datetime.timedelta(weeks = 1)
root = tk.Tk()
bls = ttk.Style()
bls.configure('Black.TLabelframe', background="#222222")
dayframe = ttk.Button(root, text="Hello", command=lambda: nextweek(curdate, one_week))
dayframe.grid(row=1, column=1, padx=5)
dayframetest = ttk.Button(root, text="test", command=lambda: printdate(curdate))
dayframetest.grid(row=2, column=1, padx=5)
root.mainloop()
我看到的迄今为止使用全局变量的例子,是有办法做到这一点而不进行CURDATE一个全球性的?
问题是因为'Button'执行函数,但它不能接收返回值,所以你不能分析'curdate'返回的值。你可以把所有的东西放在一个班上,并使用'self.'。 – furas
顺便说一句,你的python 3的语法是错误的。这个代码将不会在python 3上执行。 – Lafexlos