我对标题所说的确实遇到了麻烦。我有一点这个代码:
class eventAdd:
def __init__(self):
class agendaEvent:
def __init__(self, master):
self.frame = Frame(master, padx=10, pady=10)
稍后,在classAdd类之外,但与eventAdd处于同一级别,我有:
def addEvent():
instance = eventAdd()
doneButton = Button([PROBLEM AREA], text="Done", command=done)
我输入[问题区域]的地方显然是问题区域。我想在eventAdd里面的agendaEvent里面的框架里面创建doneButton。是否可以在类内部的init内的类中引用init内的变量,以便像这样使用?如果是这样,怎么样?
我为我可能承诺的任何编程罪行道歉。我对编程很新,我还不确定最好的方法是什么。欢迎任何建议。
提前致谢。
编辑:完整脚本:
from Tkinter import *
from functools import partial
class eventAdd:
def __init__(self):
class agendaEvent:
def __init__(self, master):
self.frame = Frame(master, padx=10, pady=10)
self.frame.grid()
self.name = Entry(self.frame)
self.name.grid(row=1, column=0)
self.time = Entry(self.frame, width=10)
self.time.grid(row=1, column=1, padx=5)
self.label1 = Label(self.frame, text="Event Name")
self.label1.grid(row=0, column=0)
self.label2 = Label(self.frame, text="Minutes")
self.label2.grid(row=0, column=1)
def addOne(master):
this_instance = agendaEvent(master)
rem = Button(this_instance.frame, text="Remove", bg="lightblue",
command=partial(remove_class, this_instance))
rem.grid(row=1, column=3)
def remove_class(instance):
instance.frame.destroy()
instance = ""
def done():
print('tbd')
window = Tk()
addOneButton = Button(window, text="+1", command=partial(addOne, window))
addOneButton.grid(row=1, column=0)
window.mainloop()
def addEvent():
instance = eventAdd()
doneButton = Button([PROBLEM AREA], text="Done", command=done)
doneButton.pack()
def done():
print('tbd')
root = Tk()
addEventButton = Button(root, text="Add Events", command=addEvent)
addEventButton.pack()
root.mainloop()
编辑:错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "C:/Users/John Grady/PycharmProjects/Dad's Agenda/scripts/main.py", line 41, in addEvent
instance = agendaEvent(window)
File "C:/Users/John Grady/PycharmProjects/Dad's Agenda/scripts/main.py", line 6, in __init__
self.frame = Frame(master, padx=10, pady=10)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2565, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2086, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: can't invoke "frame" command: application has been destroyed
答案 0 :(得分:0)
很难理解你想要完成的事情。无论如何,您将遇到的一个问题是您不应该多次调用Tk()
,而且您应该只调用mainloop()
一次。
如果您需要多个窗口,则需要创建Toplevel
的实例。任何需要销毁和重新创建根窗口的解决方案都很难做到。