1

I am new to tkinter, python, and programming in general. I have made an example program of what I'm trying to do. I am trying to use tkinter GUI to receive user inputs for date and time, then convert these tk entries into strings, then check the format of the date and time strings, then if the format is good add the date and time to a list. My issue is with converting the tk entries into strings. When I try to do so I receive an error that says "Example object has no attribute tk". In my program, I have a tk window that is made in my UserInputWindow function, and I pass this window to PromptDateTime, which is where the user is prompted to enter a date and time. When I try to convert using "dateFromUser = tk.Entry(self)", this is the part that receives the error. I don't understand why the PromptDateTime function had no problem editing the window from UserInputWindow function, yet when tk is directly referenced there is an issue.

Also: I had some trouble with formatting my code below (new to stack overflow) so please note that the first section of code is part of "class Example()", and the second section of code is the main function.

Thank you for your help! Please be nice! I'm a newbie and open to critiques.

class Example():

    #data members 
    __dateEntry = None
    __timeEntry = None

    exampleList = []


    def UserInputWindow(self, windowName, instruction):
        #Create new window to display fields and options
        new_window = tk.Tk()
        new_window.title(f'{windowName}')
        new_window.geometry = ("500x500")

        #Label to display instructions
        label_instruction = Label(new_window, text = (f'{instruction}'), font = ("Courier", 10), justify = LEFT, fg = "black", bg = "light yellow")
        label_instruction.grid(row = 0, column = 0)

        return new_window


    #this function checks to see if date string from user is in proper format, and if it is not an error window appears. 
    def VerifyDate(self, d):
        #code deleted for simplicty for this example


    #this function checks to see if time string from user is in proper format, and if it is not an error window appears.
    def VerifyTime(self, t):
        #code deleted for simplicty for this example


    #this function prompts user for date and time
    def PromptDateTime(self, new_window):
        #Label to display instructions
        label_instruction = Label(new_window, text = "Enter activity date and time: ",font = ("Courier", 10), justify = LEFT, fg = "black", bg = "light yellow")
        label_instruction.grid(row = 0, column = 0)

        #Create labels and entries for date and time
        label_date = Label(new_window, text = "Enter date in MM/DD/YYYY format: ",fg = "black", bg = "white")
        label_date.grid(row = 1, column = 0, padx = 5)
        dateEntry = Entry(new_window, fg = 'black', bg = 'white', width = 10)
        dateEntry.grid(row = 2, column = 0, padx = 5)
       
        dateFromUser = tk.Entry(self)
        str(dateFromUser)

        label_time = Label(new_window, text = "Enter time in hh:mm format (military time): ",fg = "black", bg = "white")
        label_time.grid(row = 3, column = 0, padx = 5)
        timeEntry = Entry(new_window, fg = 'black', bg = 'white', width = 10)
        timeEntry.grid(row = 4, column = 0, padx = 5)

        self.VerifyDate(dateFromUser)
        self.VerifyTime(timeEntry)

    def SubmitButton(self, new_window, new_command):
        button_submit = Button(new_window, fg = "black", bg = "light blue", text = "Submit", command = new_command)
        button_submit.grid(row = 17, column = 10, pady = 5)

    def PromptAndAddToList(self):
        window = self.UserInputWindow('Date and Time', 'Enter date and time as specified below.')
        self.PromptDateTime(window)
        self.SubmitButton(window, lambda:exampleList.append(otherClass(dateEntry, timeEntry)))

#################################################
if __name__ == '__main__':

    from tkinter import *
    import tkinter as tk

    import datetime
    
    ex = Example()

    ex.PromptAndAddToList()

    root = tk.Tk()
    root.withdraw()
    root.mainloop()
3
  • What do you think dateFromUser = tk.Entry(self) is doing? Remember that tk, like all UI systems, is event driven. It's very different from console programming. When you call PromptDateTime, nothing happens. That's just setting up your window -- it doesn't even draw it until it gets back to the main loop. If you want to get the user's input, you need to have a Button that says something like "OK", and the callback function for that button will go fetch and verify the field contents. Commented Oct 27, 2021 at 3:37
  • 1
    As the error said, the parent of dateFromUser is Example which is not a tkinter widget. Change to dateFromUser = tk.Entry(new_window) instead. Commented Oct 27, 2021 at 3:53
  • thank you both for your help and explanations. I tried that and it worked, but now it says "strptime() argument 1 must be str, not Entry". I use strptime in my VerifyDate function. I'm not sure why it's saying dateFromUser is not a string though, because after dateFromUser = tk.Entry(new_window),I did str(dateFromUser) to convert the Entry into a string. Any thoughts? Thanks again. Commented Oct 27, 2021 at 19:39

1 Answer 1

1

As the error said, the parent of dateFromUser is Example:

dateFromUser = tk.Entry(self)  # self is instance of class Example

but Example is not a tkinter widget.

Use new_window instead of self:

dateFromUser = tk.Entry(new_window)
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your help and explanation. I tried that and it worked, but now it says "strptime() argument 1 must be str, not Entry". I use strptime in my VerifyDate function. I'm not sure why it's saying dateFromUser is not a string though, because after dateFromUser = tk.Entry(new_window),I did str(dateFromUser) to convert the Entry into a string. Any thoughts? Thanks again.
@mak95 You need to use dateFromUser.get() to get the content of the entry.
unfortunately that did not work. thank you for the suggestion though.
actually, it worked! I was using the dateFromUser.get() in the wrong location in my code. I changed my logic around and now the dateFromUser.get() works. Thanks again for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.