我正在使用Tkinter创建一个简单的Gui,如何通过按钮单击打开“initia(self)”中的窗口。如何在现有接口内打开一个新窗口。不是新窗口(如果可能的话),我已经导入了必要的包(Tkinter(我使用的是Python 2.7),PIL,tkMessageBox,tkFont和tkFileDialog)我是GUI中的NOOB ....
class main1(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x210")
self.configure(bg="#00bbdc")
helv36 = tkFont.Font(family='Helvetica',size=16, weight='bold')
self.hello = Tkinter.Label(self, text="MAIN MENU", font=helv36).grid(row=1, column=2)
self.cont = Tkinter.Button(self,text="Forgot Password",font=helv36,fg='aquamarine4', command=self.initia,padx=10, pady=10)
h = self.cont
h.grid(row=3, column=2,padx=10)
h.config(bd=8, relief='raised')
def initia(self): """ This Function shows Attribute Error, How can I define THIS in a Class """
root = Tkinter.Tk()
# frame = Tkinter.Frame(self.root, bd=2, relief='raised')
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
xscroll = Tkinter.Scrollbar(self, orient='horizontal')
self.xscroll.grid(row=1, column=0, sticky='ew')
yscroll = Tkinter.Scrollbar(self)
self.yscroll.grid(row=0, column=1, sticky='ns')
canvas = Tkinter.Canvas(self, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
self.canvas.grid(row=0, column=0, sticky='nsew')
self.xscroll.config(command=canvas.xview)
self.yscroll.config(command=canvas.yview)
self.pack(fill='both',expand=1)
#adding the image
File = askopenfilename(parent=root, initialdir="C:/",title='Choose an image.')
img = ImageTk.PhotoImage(Image.open(File))
self.canvas.create_image(0,0,image=img,anchor="nw")
self.canvas.config(scrollregion=canvas.bbox('all'))
#function to be called when mouse is clicked
def printcoords(event):
#outputting x and y coords to console
print (event.x,event.y)
#mouseclick event
canvas.bind("<Button 1>",printcoords)
if __name__ == "__main__":
app = main1(None)
app.title('Main Form')
app.mainloop()
如何将“initia(self)”功能添加到新类