tkinter 如何将框架分成几部分



在此处输入图像描述我正在制作tkinter gui程序。在程序中,当我单击按钮时,将显示一个弹出窗口。我想将弹出框分成 9 个按钮并在按钮中显示图像。但是在我的代码中,按钮是连接的,而不是分开的。下面是我代码的一部分。我会等你的帮助。谢谢。

  def create_window(self):
        window=Toplevel(root2)
        window.geometry("600x600")
        self.galleryframe=Frame(window)
        self.galleryframe.pack()
        self.topgframe=Frame(self.galleryframe,width=600,height=200)
        self.topgframe.pack()
        self.ibutton1=Button(self.topgframe,width=20,height=20)
        try:
            self.image1=PhotoImage(file="/home/som/imagefolder/1.png")
            self.ibutton1.config(image=self.image1)
            self.ibutton1.image=self.image1
        except TclError:
            pass
        self.ibutton1.pack(side=LEFT)
        self.ibutton2=Button(self.topgframe,width=20,height=20)
        try:
            self.image2=PhotoImage(file="/home/som/imagefolder/2.png")
            self.ibutton2.config(image=self.image2)
            self.ibutton2.image=self.image2
        except TclError:
            pass
        self.ibuttom2.pack()
        self.ibutton3=Button(self.topgframe,width=20,height=20)
        try:
            self.image3=PhotoImage(file="/home/som/imagefolder/3.png")
            self.ibutton3.config(image=self.image3)
            self.ibutton3.image=self.image3
        except TclError:
            pass
        self.ibuttom3.pack(side=RIGHT)
        self.midgframe=Frame(self.galleryframe,width=600,height=200)
        self.midgframe.pack()
        self.ibutton4=Button(self.midgframe,width=20,height=20)
        try:
            self.image4=PhotoImage(file="/home/som/imagefolder/4.png")
            self.ibutton4.config(image=self.image4)
            self.ibutton4.image=self.image4
        except TclError:
            pass
        self.ibuttom4.pack(side=LEFT)
      ....ibutton5-9 are almost same with upper things.

代码中有一个小拼写错误。尝试更换:

self.ibuttom2.pack()

self.ibutton2.pack()   # Same for buttons 3, 4, etc.

相关内容

  • 没有找到相关文章

最新更新