tkinter如何将框架分成几部分?

时间:2017-06-11 14:53:19

标签: python button tkinter

enter image description here我正在制作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.

1 个答案:

答案 0 :(得分:0)

您的代码中有一个小错字。尝试更换:

self.ibuttom2.pack()

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