woojung kim woojung kim -4 years ago 171
Python Question

tkinter how to divide frame into parts?

enter image description hereI am making tkinter gui program. In the program, When I clicked button, A pop-up is displayed.
I want to divide pop-up frame into 9 buttons and display image in button.
But in my code, The buttons are joined, not divided.
Below is part of my code. I will wait for your help. Thank you.

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.

Answer Source

There is a small typo in your code. Try replacing:

self.ibuttom2.pack()

with

self.ibutton2.pack()   # Same for buttons 3, 4, etc.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download