Python/Tinker 'datetime.date' 对象没有属性 'get' (在记事本文档中输入日期)
Python/Tinker 'datetime.date' object has no attribute 'get' (Input date in to notepad doc)
我是新来的
为了了解基本编码的工作原理,我创建了一个 GUI,最终用户可以在其中将文本输入到输入小部件中,按下按钮,文本将显示在 shell 上保存记事本。
我的 GUI 有三个按钮;
- 按钮 1 会将在 GUI 中输入的文本保存到记事本并通过 shell
显示
- 按钮2是将今天的日期保存到记事本并通过shell
显示
- 按钮 3 打开记事本 .exe 文件
我能够成功地将文本保存到记事本中并通过 GUI 应用程序打开记事本。但是,我无法在记事本中保存日期。
这是显示属性错误的代码示例。(单击按钮 2 时)
#030820, printing time to shell and notepad # in progress
def button1Click():
print (time.get)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.get() + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")
这是显示的错误
File "C:\Users\shane\AppData\Local\Programs\Python\Python38-
32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\shane\source\repos\GUI input to Notepad\GUI input to
Notepad\GUI_input_to_Notepad.py", line 16, in button1Click
print (time.get)
AttributeError: 'datetime.date' object has no attribute 'get'
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
The program 'python.exe' has exited with code -1 (0xffffffff).
这是我的应用程序的完整代码
import tkinter as tk
import datetime
import os #opens a text file
window = tk.Tk()
window.title ("Shane's Text to NotePad Testing GUI")
window.geometry("400x150")
window.configure(bg="#3BB9FF")
time = datetime.date.today()
print (time)
#030820, printing time to shell and notepad # in progress
def button1Click():
print (time.get)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.get() + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")
#button3 opens txt file
def button3Click():
os.system("Names101.txt")
button3 = tk.Button(window, text=("Open Notepad file"),width=30, command=button3Click, bg="light green")
#030820, printing entry1 to shell and notepad
entry1 = tk.Entry(window, width = 50)
def entry1get():
print (entry1.get()) #prints to shell
with open("Names101.txt", "a") as output: #prints to notepad
output.write(entry1.get() + "\n") #prints to notepad
button2 = tk.Button(window, text="Input text to notepad", width=30, command=entry1get, bg="#E6E6FA")
#030820 Label one
label1 = tk.Label(text="Enter text below", font="bold" , bg="#3BB9FF")
label1.pack()
entry1.pack()
button2.pack()
button1.pack()
button3.pack()
window.mainloop()
`
Lewis Morris 回答的问题,以下是解析
def button1Click():
print (time.strftime("%Y-%m-%d"))
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.strftime("%Y-%m-%d") + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30,
command=button1Click, bg="light blue")
打印日期时可以使用.strftime()
方法。
这允许您以任何您希望的方式打印和格式化您的日期时间。
即
初始化你的日期。
time = datetime.date.today()
然后使用传递的参数调用 strftime(),例如
time.strftime("%Y-%m-%d")
然后打印如下
print(time.strftime("%Y-%m-%d"))
可在此处找到完整的 strftime 代码列表
https://www.programiz.com/python-programming/datetime/strftime
变量time
初始化为:
time = datetime.date.today()
根据 datetime docs,没有对应于您对 time.get
或 time.get()
的使用的属性或 class 方法。
此更改应该可以解决您的问题:
def button1Click():
print (time)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(str(time) + "\n") #prints to notepad
我是新来的
为了了解基本编码的工作原理,我创建了一个 GUI,最终用户可以在其中将文本输入到输入小部件中,按下按钮,文本将显示在 shell 上保存记事本。
我的 GUI 有三个按钮;
- 按钮 1 会将在 GUI 中输入的文本保存到记事本并通过 shell 显示
- 按钮2是将今天的日期保存到记事本并通过shell 显示
- 按钮 3 打开记事本 .exe 文件
我能够成功地将文本保存到记事本中并通过 GUI 应用程序打开记事本。但是,我无法在记事本中保存日期。
这是显示属性错误的代码示例。(单击按钮 2 时)
#030820, printing time to shell and notepad # in progress
def button1Click():
print (time.get)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.get() + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")
这是显示的错误
File "C:\Users\shane\AppData\Local\Programs\Python\Python38-
32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\shane\source\repos\GUI input to Notepad\GUI input to
Notepad\GUI_input_to_Notepad.py", line 16, in button1Click
print (time.get)
AttributeError: 'datetime.date' object has no attribute 'get'
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
The program 'python.exe' has exited with code -1 (0xffffffff).
这是我的应用程序的完整代码
import tkinter as tk
import datetime
import os #opens a text file
window = tk.Tk()
window.title ("Shane's Text to NotePad Testing GUI")
window.geometry("400x150")
window.configure(bg="#3BB9FF")
time = datetime.date.today()
print (time)
#030820, printing time to shell and notepad # in progress
def button1Click():
print (time.get)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.get() + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")
#button3 opens txt file
def button3Click():
os.system("Names101.txt")
button3 = tk.Button(window, text=("Open Notepad file"),width=30, command=button3Click, bg="light green")
#030820, printing entry1 to shell and notepad
entry1 = tk.Entry(window, width = 50)
def entry1get():
print (entry1.get()) #prints to shell
with open("Names101.txt", "a") as output: #prints to notepad
output.write(entry1.get() + "\n") #prints to notepad
button2 = tk.Button(window, text="Input text to notepad", width=30, command=entry1get, bg="#E6E6FA")
#030820 Label one
label1 = tk.Label(text="Enter text below", font="bold" , bg="#3BB9FF")
label1.pack()
entry1.pack()
button2.pack()
button1.pack()
button3.pack()
window.mainloop()
`
Lewis Morris 回答的问题,以下是解析
def button1Click():
print (time.strftime("%Y-%m-%d"))
with open("Names101.txt", "a") as output: #prints to notepad
output.write(time.strftime("%Y-%m-%d") + "\n") #prints to notepad
button1 = tk.Button(window, text=("Record Time"),width = 30,
command=button1Click, bg="light blue")
打印日期时可以使用.strftime()
方法。
这允许您以任何您希望的方式打印和格式化您的日期时间。
即
初始化你的日期。
time = datetime.date.today()
然后使用传递的参数调用 strftime(),例如
time.strftime("%Y-%m-%d")
然后打印如下
print(time.strftime("%Y-%m-%d"))
可在此处找到完整的 strftime 代码列表
https://www.programiz.com/python-programming/datetime/strftime
变量time
初始化为:
time = datetime.date.today()
根据 datetime docs,没有对应于您对 time.get
或 time.get()
的使用的属性或 class 方法。
此更改应该可以解决您的问题:
def button1Click():
print (time)
with open("Names101.txt", "a") as output: #prints to notepad
output.write(str(time) + "\n") #prints to notepad