Python Tkinter在现有表单中打开新表单,属性错误
我正在使用Tkinter制作一个简单的Gui,如何通过单击按钮在“initia(self)”中打开窗口。如何在现有界面内打开新窗口。不是新窗口(如果可能的话),我已经导入了必要的包(Tkinter(我使用的是Python 2.7)、PIL、tkMessageBox、tkFont和tkFileDialog),我是GUI中的NOOBPython Tkinter在现有表单中打开新表单,属性错误,python,python-2.7,tkinter,tk,Python,Python 2.7,Tkinter,Tk,我正在使用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,pa
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()
class main1(Tkinter.Tk):
定义初始化(自身,父级):
Tkinter.Tk.\uuuuu init\uuuuuu(自,父)
self.parent=parent
self.initialize()
def初始化(自):
自几何(“600x210”)
自我配置(bg=“#00bbdc”)
helv36=tkFont.Font(family='Helvetica',size=16,weight='bold')
self.hello=Tkinter.Label(self,text=“MAIN MENU”,font=helv36).grid(行=1,列=2)
self.cont=Tkinter.Button(self,text=“忘记密码”,font=helv36,fg='aquamarine4',command=self.initia,padx=10,pady=10)
h=自我控制
h、 网格(行=3,列=2,padx=10)
h、 配置(bd=8,relief='raised')
def initia(self):“此函数显示属性错误,如何在类中定义此错误?”
root=Tkinter.Tk()
#frame=Tkinter.frame(self.root,bd=2,relief='raised')
self.grid_rowconfigure(0,权重=1)
self.grid\u column配置(0,权重=1)
xscroll=Tkinter.Scrollbar(self,orient='horizontal')
self.xscroll.grid(行=1,列=0,sticky='ew')
yscroll=Tkinter.Scrollbar(自)
self.yscroll.grid(行=0,列=1,sticky='ns')
canvas=Tkinter.canvas(self,bd=0,xscrollcommand=xscroll.set,yscrollcommand=yscroll.set)
self.canvas.grid(行=0,列=0,sticky='nsew')
self.xscroll.config(命令=canvas.xview)
self.yscroll.config(命令=canvas.yview)
self.pack(fill='both',expand=1)
#添加图像
File=askopenfilename(parent=root,initialdir=“C:/”,title='Choose an image')
img=ImageTk.PhotoImage(Image.open(文件))
self.canvas.create_image(0,0,image=img,anchor=“nw”)
self.canvas.config(scrollregion=canvas.bbox('all'))
#单击鼠标时要调用的函数
def printcoords(事件):
#将x和y坐标输出到控制台
打印(事件x、事件y)
#鼠标点击事件
canvas.bind(“,printcoords)
如果名称=“\uuuuu main\uuuuuuuu”:
app=main1(无)
附录标题(“主表格”)
app.mainloop()
如何将函数“initia(self)”放入一个新类您正试图在
initia
中创建Tkinter.Tk
的第二个实例。这是行不通的。例如,在同一窗口中无法打开其他实例??请仔细阅读链接。措辞正确:无法在同一个程序中创建另一个实例。谢谢,但我已将root=Tk()
更改为root=Toplevel()
仍然会发生错误!!!错误?什么错误?你的问题中没有提到任何错误