我想創建一個對話框,將從彈出對話框中獲得一個社會安全號碼(或simular輸入),但是當我嘗試我得到一個錯誤說,類沒有該屬性。下面是代碼:訪問一個變量,從一個不同的類 - 自定義對話框
from Tkinter import *
class App:
def __init__(self, master):
b = Button(text="Click for social dialog", command=self.getSocial)
b.grid(row=0, column=0)
def getSocial(self):
d = socialDialog(root)
print d.social
class socialDialog:
def __init__(self, master):
self.top = Toplevel()
Label(self.top, text='Social Security #: ').grid(row=0, column=0)
self.entry = Entry(self.top)
self.entry.grid(row=0, column=1)
self.entry.focus_set()
self.top.bind('<Key>', self.formatData)
self.top.bind('<Return>', self.ok)
def formatData(self, master):
currentData = self.entry.get()
if len(currentData) == 3:
self.entry.insert(3, '-')
elif len(currentData) == 6:
self.entry.insert(6, '-')
elif len(currentData) > 11:
self.entry.delete(-1, END)
def ok(self, master):
self.social = self.entry.get()
self.top.destroy()
root = Tk()
app = App(root)
root.mainloop()
我能這次來弄明白,但它是很容易幫助您一個錯誤,如果你打印錯誤堆棧跟蹤 - 這有它寶貴的調試信息! – Brionius