1

私は少し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()

userDBにはこれがあります

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! 

どんな助けでも大歓迎です、忍耐を持ってくれてありがとう。

4

1 に答える 1

1

days日付を減算するときに取得する timedelta オブジェクトのプロパティを使用します。

意味:

a) 削除するchangeInDateStr

b)changeInDateグローバルにする

c) 印刷行を次のように変更します。

print [..blah blah...] " This was " + changeInDate.days + [" days", " day"][changeInDays.days==1] + " ago!"
于 2013-09-27T19:51:32.553 に答える