Home > Software engineering >  tkinter/datetime/weekday/dictionary/ type error : strptime() argument 1 must be str, not entry
tkinter/datetime/weekday/dictionary/ type error : strptime() argument 1 must be str, not entry

Time:02-22

  1. In function (def wky(event)), without 'global ab', it happens next error message : 'UnboundLocalError'. And then, I used that words(global ab). But, I wonder that that words is necessary? Without declare the global in function, we can't use the local variable?

  2. In function, is there wrong sentence? I used the Dictionary. I thought that the appropriate value is coming in Dictionary. But, when I run the below code, it happened the Error message. What I miss something?

  3. In conclusion, this code is that label's value come from in ab valiable. ab valiable's initial value is None. User immediately input any value, and then label's value is automatically inserted following press 'Enter'key.

I hope for your merciful help ~ ^^

Below the paragrape, I wrote the code.


from tkinter import *

from datetime import datetime

root = Tk()

root.title("Moving")

root.geometry("640x480")

root.resizable(False, False)

def wky(event):

    global ab

    ab = datetime.strptime(ab, "%Y-%m-%d")

    dateDict = {0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat", 6:"Sun"}

    dateDict[ab.weekday()]

    label.config(text = dateDict[ab.weekday()])

ab = Entry(root)

ab.bind("<Return>", wky)

ab.pack()

label=Label(root)

label.pack()

root.mainloop()

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__return self.func(*args)
  File "c:\Users\JS\Desktop\PYTHONWORKSPACE\movingproject\1_name.py", line 145, in wky
    ab = datetime.strptime(ab, "%Y-%m-%d")
TypeError: strptime() argument 1 must be str, not Entry

CodePudding user response:

As the error said, ab is an Entry widget that cannot be used in strptime(). You need to use the value in the Entry widget instead.

Also better use another local variable to store the result of strptime() instead of using ab:

def wky(event):
    date = datetime.strptime(ab.get(), "%Y-%m-%d")
    dateDict = {0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat", 6:"Sun"}
    #dateDict[date.weekday()] # this line is useless
    label.config(text=dateDict[date.weekday()])
  • Related