Hi all,
i was writing a gui for a small desktop application i made as a part of learning python.I use python 2.7.I used Tkinter.I am new to python and this was my first trial with Tkinter.

First i created 9 buttons arranged in the form of a 3*3 matrix and set the image of the button as None.

frame=Frame(root,name='frame')
frame.pack(fill=BOTH, expand=1)

cols=1
rows=1
for i in range(1,10):
l=Button(frame,name=str(i),bg='red',fg='green',background='blue',width=8,height=4,image=None)
l.bind("<Button-1>",self.button_handle)
l.grid(row = rows, column =cols,sticky = W+E+N+S,)
self.label_list.append(l)

if i%3:
cols+=1
else:
rows+=1
cols=1

what i wanted to do is when i click on any of the buttons an image need to be displayed on the button.
for this i used another function.

img=PhotoImage(file="./cross.GIF")
def change_widget_icon(self,widget,img):
if widget==None:
widget=img

The problem i face is that when all the 9 buttons are filled with images the whole frame containing the grid of buttons shrinks and move to the center of the parent container frame


Thanks in advance

Recommended Answers

All 4 Replies

Just a few quick observations:
background is the same as bg
img might best be self.img
how to you go from the button to widget
give us the full class so we can help

Using i and I as variable names spell trouble in any code

Thank you for th response.I am posting the full code.
The problem i have is that when a row or column of the 3*3 grid is filled in with image that row/column shrinks.If fill all the buttons with images the whole grid shrinks to a small square and the contents are not visible

from Tkinter import *

class ui:

    def __init__(self):
        self.label_list=[]
        self.cross_image = None
        self.zero_image = None
        self.name=None
        self.window_root=self.make_gui()
        self.window_root.mainloop()



    def button_handle(self,e):
            if not e.widget['image']:
                print "widget already has an image"
                e.widget['image']=self.cross_image
            elif  e.widget['image'] == self.cross_image :
                 e.widget['image']=self.zero_image
            else:
                 e.widget['image']=self.cross_image


    def make_gui(self):
        root=Tk()
        self.cross_image = PhotoImage(file="./cross.GIF")
        self.zero_image = PhotoImage(file="./zero.GIF")
        frame_top=Frame(root,name='frame_top')
        frame_top.pack()

        name_label  = Label ( frame_top,text='Name' )
        name_label.grid(row = 1, column =0)

        self.name = Entry ( frame_top, bg='light blue' ,)
        self.name.grid(row = 1, column =1)


        frame=Frame(root,name='frame')
        frame.pack(fill=BOTH, expand=1)
        cols=1
        rows=1
        for i in range(1,10):
            l=Button(frame,name=str(i),bg='red',fg='green',background='blue',width=8,height=4,image=None)
            l.bind("<Button-1>",self.button_handle)
            l.grid(row = rows, column =cols,sticky = W+E+N+S,)
            self.label_list.append(l)

            if i%3:
                cols+=1
            else:
                rows+=1
                cols=1

        return root

ui()
frame_top=Frame(root,name='frame_top')
frame_top.pack()
 
name_label = Label ( frame_top,text='Name' )
name_label.grid(row = 1, column =0)

You can not use both grid() and pack() in the same program. See the Warning here, and then pick one or the other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.