简体   繁体   English

Python Tkinter:对象没有属性 tk

[英]Python Tkinter: object has no attribute tk

I am new to tkinter, python, and programming in general.我是 tkinter、python 和编程的新手。 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.我正在尝试使用 tkinter GUI 接收用户输入的日期和时间,然后将这些 tk 条目转换为字符串,然后检查日期和时间字符串的格式,如果格式良好,则将日期和时间添加到列表中。 My issue is with converting the tk entries into strings.我的问题是将 tk 条目转换为字符串。 When I try to do so I receive an error that says "Example object has no attribute tk" .当我尝试这样做时,我收到一条错误消息,指出“示例对象没有属性 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.在我的程序中,我有一个在 UserInputWindow 函数中创建的 tk 窗口,我将这个窗口传递给 PromptDateTime,这是提示用户输入日期和时间的地方。 When I try to convert using "dateFromUser = tk.Entry(self)", this is the part that receives the error.当我尝试使用“dateFromUser = tk.Entry(self)”进行转换时,这是收到错误的部分。 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.我不明白为什么 PromptDateTime 函数从 UserInputWindow 函数编辑窗口没有问题,但是当直接引用 tk 时会出现问题。

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.另外:我在格式化下面的代码时遇到了一些问题(堆栈溢出的新手)所以请注意,代码的第一部分是“class Example()”的一部分,而第二部分代码是主函数。

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

As the error said, the parent of dateFromUser is Example :正如错误所说, dateFromUser的父dateFromUserExample

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

but Example is not a tkinter widget.Example不是 tkinter 小部件。

Use new_window instead of self :使用new_window而不是self

dateFromUser = tk.Entry(new_window)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM