Home > Software engineering >  Python Tkinter: object has no attribute tk
Python Tkinter: object has no attribute tk

Time:10-28

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()

CodePudding user response:

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)
  • Related