gpt4 book ai didi

python - 我的日期时间格式不正确,我做错了什么?

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:59 24 4
gpt4 key购买 nike

我是一个Python新手,但找不到有关这个特定问题的任何信息。

from Tkinter import *
from datetime import datetime

username = []
password = []
userClass = []
userDB = []

def loadLoginWindow():
global loginWindow, indicator, usernameEntry, passwordEntry, indicatorLabel, submitButton, loadButton

loginWindow = Tk()
loginWindow.wm_title("Project 001: Login")
loginWindow.wm_resizable(0,0)

indicator = StringVar()

usernameLabel = Label(loginWindow, text = "username:")
usernameLabel.grid(row = 0, column = 0)

usernameEntry = Entry(loginWindow)
usernameEntry.grid(row = 0, column = 1)

passwordLabel = Label(loginWindow, text = "password:")
passwordLabel.grid(row = 1, column = 0)

passwordEntry = Entry(loginWindow, show = "*")
passwordEntry.grid(row = 1, column = 1)

indicatorLabel = Label(loginWindow, textvariable = indicator)
indicatorLabel.grid(row = 2, column = 0, columnspan = 2)

submitButton = Button(loginWindow, text = "submit", command = login)
submitButton.grid(row = 3, column = 0, columnspan = 2)

loginWindow.mainloop()

def loadUserPanelWindow():

global userPanelWindow, headerGIF, spacerGIF, mainLabelText, usrnm

mainLabelText()

userPanelWindow = Tk()
userPanelWindow.wm_title("Project 001: User Panel")
userPanelWindow.wm_resizable(0,0)

headerGIF = PhotoImage(file = "image/userPanel/header.gif")
spacerGIF = PhotoImage(file = "image/userPanel/spacer.gif")

headerLabel = Label(userPanelWindow, image = headerGIF)
headerLabel.grid(row = 0, column = 0, columnspan = 6)

numberButton = Button(userPanelWindow, text= "Number")
numberButton.grid(row = 1, column = 0)

algebraButton = Button(userPanelWindow, text= "Algebra")
algebraButton.grid(row = 1, column = 1)

dataButton = Button(userPanelWindow, text= "Data")
dataButton.grid(row = 1, column = 2)

shapeButton = Button(userPanelWindow, text= "Shape")
shapeButton.grid(row = 1, column = 3)

spaceButton = Button(userPanelWindow, text= "Space")
spaceButton.grid(row = 1, column = 4)

measuresButton = Button(userPanelWindow, text= "Measures")
measuresButton.grid(row = 1, column = 5)

spacerLabel = Label(userPanelWindow, image = spacerGIF)
spacerLabel.grid(row = 2, column = 0, columnspan = 6)

mainLabel = Label(userPanelWindow, text = mainLabelText, justify = LEFT)
mainLabel.grid(row = 3, column = 0, columnspan = 6, sticky = W)

spacerLabel = Label(userPanelWindow, image = spacerGIF)
spacerLabel.grid(row = 4, column = 0, columnspan = 6)

settingsButton = Button(userPanelWindow, text= "Settings")
settingsButton.grid(row = 5, column = 3)

helpButton = Button(userPanelWindow, text= "Help")
helpButton.grid(row = 5, column = 4)

logoutButton = Button(userPanelWindow, text= "Logout", command = logout)
logoutButton.grid(row = 5, column = 5)

userPanelWindow.mainloop()

def login():
global index, usrnm, psswrd

index = 0
usrnm = usernameEntry.get()
psswrd = passwordEntry.get()

while index < len(username):
if username[index] == usrnm:
if password[index] == psswrd:
loginWindow.destroy()
loadUserDB()
updateUserDBDates()
loadUserPanelWindow()
break
else:
indicator.set("Password doesn't exist!");
break
else:
index = index + 1
else:
indicator.set("Username doesn't exist!");

def logout():
global index, usrnm, psswrd

index = 0
usrnm = 0
psswrd = 0

userPanelWindow.destroy()

loadLoginDB()
loadLoginWindow()

def saveData():
global username, password, userClass

with open("username.txt", "w") as fWUsername:
fWUsername.write("\n".join(str(x) for x in username))

with open("password.txt", "w") as fWPassword:
fWPassword.write("\n".join(str(x) for x in password))

with open("userclass.txt", "w") as fWUserClass:
fWUserClass.write("\n".join(str(x) for x in userClass))

def loadLoginDB():
global username, password, userClass

with open("username.txt", "r") as fRUsername:
usernameNoStrip = fRUsername.readlines()
username = map(str.strip, usernameNoStrip)

with open("password.txt", "r") as fRPassword:
passwordNoStrip = fRPassword.readlines()
password = map(str.strip, passwordNoStrip)

with open("userClass.txt", "r") as fRUserClass:
userClassNoStrip = fRUserClass.readlines()
userClass = map(str.strip, userClassNoStrip)

def loadUserDB():
global usrnm, userDB

with open("userDB/" + usrnm + ".txt", "r") as fRUserDB:
userDBNoString = fRUserDB.readlines()
userDB = map(str.strip, userDBNoString)

def updateUserDBDates():
global userDB, currentDate, previousDate, changeInDateStr

index = 0
index2 = 0

currentDate = datetime.strptime(userDB[0], "%Y-%m-%d")
previousDate = datetime.strptime(userDB[1], "%Y-%m-%d")
changeInDate = currentDate - previousDate

changeInDateStr = str(changeInDate)

def mainLabelText():
global mainLabelText, usrnm, currentDate, previousDate, changeInDateStr

mainLabelText = "Welcome, " + usrnm + "!" + "\n" + "You were last logged on at " + str(previousDate) + "." + " This was " + changeInDateStr + " ago!"

loadLoginDB()
loadLoginWindow()

在用户数据库中我有这个

2013-09-25
2013-09-20

但在 mainLabel 中我得到了这个

you were last logged on at 2013-09-20. This was 5 days, 0:00:00 ago!`

预期输出是

you were last logged on at 2013-09-20. This was 5 days, ago! 

任何帮助将不胜感激,感谢您的耐心等待。

最佳答案

使用减去日期时获得的 timedelta 对象中的 days 属性。

含义:

a) 删除 changeInDateStr

b) 使 changeInDate 全局化

c) 将打印行更改为:

print [..blah blah...] " This was " + changeInDate.days + [" days", " day"][changeInDays.days==1] + " ago!"

关于python - 我的日期时间格式不正确,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com