Python 使用按钮更新变量
因此,每次用户点击一个按钮时,我都会给一个日期加上7天。 以下是我的代码的简化版本:Python 使用按钮更新变量,python,tkinter,Python,Tkinter,因此,每次用户点击一个按钮时,我都会给一个日期加上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 imp
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成为全局变量的情况下实现这一点?通常,使用面向对象的方法来创建tkinter应用程序会更好。我以您的示例为例,对其进行了修改,使curdate存储在App类中 注意,我在蟒蛇3上测试过
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
import tkinter.ttk as ttk
import datetime
import calendar
class App(tk.Frame):
def __init__(self,master=None,**kw):
tk.Frame.__init__(self,master=master,**kw)
bls = ttk.Style()
bls.configure('Black.TLabelframe', background="#222222")
self.dayframe = ttk.Button(self, text="Hello", command= self.nextweek)
self.dayframe.grid(row=1, column=1, padx=5)
self.dayframetest = ttk.Button(self, text="test", command= self.printdate)
self.dayframetest.grid(row=2, column=1, padx=5)
self.curdate = datetime.date(2016, 1, 4)
self.one_week = datetime.timedelta(weeks = 1)
def nextweek(self):
self.curdate = (self.curdate + self.one_week)
def printdate(self):
print(self.curdate)
if __name__ == '__main__':
root = tk.Tk()
App(root).grid()
root.mainloop()
通常最好使用面向对象的方法来创建tkinter应用程序。我以您的示例为例,对其进行了修改,使curdate存储在App类中 注意,我在蟒蛇3上测试过
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
import tkinter.ttk as ttk
import datetime
import calendar
class App(tk.Frame):
def __init__(self,master=None,**kw):
tk.Frame.__init__(self,master=master,**kw)
bls = ttk.Style()
bls.configure('Black.TLabelframe', background="#222222")
self.dayframe = ttk.Button(self, text="Hello", command= self.nextweek)
self.dayframe.grid(row=1, column=1, padx=5)
self.dayframetest = ttk.Button(self, text="test", command= self.printdate)
self.dayframetest.grid(row=2, column=1, padx=5)
self.curdate = datetime.date(2016, 1, 4)
self.one_week = datetime.timedelta(weeks = 1)
def nextweek(self):
self.curdate = (self.curdate + self.one_week)
def printdate(self):
print(self.curdate)
if __name__ == '__main__':
root = tk.Tk()
App(root).grid()
root.mainloop()
问题是因为
按钮执行函数,但它不能接收返回值,所以您不能访问curdate
返回值。您可以将所有内容放在一个类中并使用self.
。顺便说一句,您的Python3语法是错误的。此代码不会在python 3上执行。问题在于按钮执行函数,但它不能接收返回值,所以您不能分配curdate
返回值。您可以将所有内容放在一个类中并使用self.
。顺便说一句,您的Python3语法是错误的。这段代码不会在Python3上执行。谢谢,这是可行的,但是按钮将不得不更新来自不同对象的更多变量…因此将所有内容放在一个大类中可能会很混乱…这就是为什么要使用面向对象编程。App类可以包含其他类的实例。例如,您可能有一个类用于存储日期,一个类用于存储人员列表,另一个类用于存储清单。您甚至可以有一个类的实例列表。然后,当按下按钮时,App类可以为每个类调用update方法。这样做比在代码中使用随机变量要好得多。将相似的变量和修改它们的方法分组到类中。谢谢,这是可行的,但是按钮将不得不更新来自不同对象的更多变量…因此将所有内容放在一个大类中可能会很混乱…这就是为什么要使用面向对象编程。App类可以包含其他类的实例。例如,您可能有一个类用于存储日期,一个类用于存储人员列表,另一个类用于存储清单。您甚至可以有一个类的实例列表。然后,当按下按钮时,App类可以为每个类调用update方法。这样做比在代码中使用随机变量要好得多。将相似的变量和修改它们的方法分组到类中。