[英]Python Tkinter: object has no attribute tk
我是 tkinter、python 和編程的新手。 我已經制作了一個我正在嘗試做的示例程序。 我正在嘗試使用 tkinter GUI 接收用戶輸入的日期和時間,然后將這些 tk 條目轉換為字符串,然后檢查日期和時間字符串的格式,如果格式良好,則將日期和時間添加到列表中。 我的問題是將 tk 條目轉換為字符串。 當我嘗試這樣做時,我收到一條錯誤消息,指出“示例對象沒有屬性 tk” 。 在我的程序中,我有一個在 UserInputWindow 函數中創建的 tk 窗口,我將這個窗口傳遞給 PromptDateTime,這是提示用戶輸入日期和時間的地方。 當我嘗試使用“dateFromUser = tk.Entry(self)”進行轉換時,這是收到錯誤的部分。 我不明白為什么 PromptDateTime 函數從 UserInputWindow 函數編輯窗口沒有問題,但是當直接引用 tk 時會出現問題。
另外:我在格式化下面的代碼時遇到了一些問題(堆棧溢出的新手)所以請注意,代碼的第一部分是“class Example()”的一部分,而第二部分代碼是主函數。
感謝您的幫助! 請友好一點! 我是新手,樂於接受批評。
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()
正如錯誤所說, dateFromUser
的父dateFromUser
是Example
:
dateFromUser = tk.Entry(self) # self is instance of class Example
但Example
不是 tkinter 小部件。
使用new_window
而不是self
:
dateFromUser = tk.Entry(new_window)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.