Welcome, guest | Sign In | My Account | Store | Cart

This is a GUI multiplication table. The labels and buttons are created in loops, yet each button has an associated value.

Python, 46 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from Tkinter import *

class Application(Frame):
    """Multiplication Table"""
   
    def __init__(self,master):
        """initialize the frame"""
        Frame.__init__(self,master)
        self.grid()
        
        
        for i in range(12):
            val=i+1
            Label(self,text=val).grid(row=val,column=0)
            Label(self,text=val).grid(row=0,column=val)

        self.btns=[]
        for i in range(12):
            btns=[]
            for j in range(12):
                btns.append(self.create_widgets(i,j))
            self.btns.append(btns)
        
            

    def create_widgets(self,a,b):
        i=(a+1)*(b+1)
        bttn=Button(self,text="?",height=2,width=4)
        def button_action2(event,self=self,i=i):
            return self.button_action(event,i)
        bttn.bind("<ButtonRelease-1>",button_action2)
        bttn.grid(row=a+1,column=b+1)
 
           
    def button_action(self,ev,i):
        bttn=ev.widget
        bttn.configure(text=str(i),fg='red')
    
        

root=Tk()
root.title("Multiplication Table")
app=Application(root)


root.mainloop()