2

it's my first post here. I'm trying to create a 5x5 table with random numbers. The goal is that the user is supposed to click from the smallest to biggest number on the table, and once he clicked correct one it should be disabled. I don't want to attach each button to variable. I've created windows with random number, but now I'd like to create function which would check if the clicked number is the smallest, and if the answer is yes I have to change it's state to DISABLED. I been sitting on this for over 4 hours and I have no idea how to access clicked button information like 'text', and how can I disable it after user clicks on it.

Here's what I got so far, working on dat function.

from tkinter import *
import random


def click(z=None):
    global o
    Button(state=DISABLED)


o=Tk()
y=0
listrow=[4,3,2,1,0]
numbers=[]
spr=IntVar()

while len(numbers) < 25:
    r = random.randint(0,999)
    if r not in numbers:
        numbers.append(r)

for i in range(1,26):
    Button(o, text=str(numbers[i-1]), width=10).grid(row=listrow[i%5], column=y)
    if i == 5:
        y+=1
    elif i == 10:
        y+=1
    elif i==15:
        y+=1
    elif i==20:
        y+=1
    else:
        continue

o.bind_all('<Button-1>', click) 


o.mainloop()
Yasha Doe
  • 33
  • 4
  • 1
    Four ways, either you assign the button a variable like what you're supposed to do, or use the IntVar you made. Or save all the buttons to a list, or create a subclass Button and add your own methods. – Taku Apr 15 '17 at 15:33
  • There is a "text" value assigned to each button. What can I type in function, so once user will click on that button I will be able to use this "text" value? cget does not seem to work if button is not attached to variable. – Yasha Doe Apr 15 '17 at 16:30
  • You can use the text attribute only if you knew what button was pressed, which you don't right now – Taku Apr 15 '17 at 16:33

1 Answers1

0

Consider integrating a class object with defined modules for building controls and the button click event. Specifically, on each button click check the minimum of the numbers list and disable corresponding button item by passing the index integer as parameter. See adjustment below.

Credit to @BrenBarn for the clever lambda solution lambda i=i in button command call:

from tkinter import *
import random

class NUMapp():        
    def __init__(self):
        self.root = Tk()
        self.buildControls()
        self.root.mainloop()

    def buildControls(self):
        self.root.wm_title("Random Number Picker") 
        x = 0; y = 0
        self.numbers = []; self.numbtn = []

        while len(self.numbers) < 25:
            r = random.randint(0,999)
            if r not in self.numbers:
                self.numbers.append(r)

        for i in range(1,26):
            self.numbtn.append(Button(self.root, text=str(self.numbers[i-1]), width=10, 
                               command=lambda i=i:self.btnclick(i-1)))
            self.numbtn[i-1].grid(row=x, column=y)    
            x+=1    
            if i % 5 == 0:
                x = 0
                y += 1

    def btnclick(self, mynum):
        currnum = int(self.numbtn[mynum].cget('text'))     # CAPTURE BUTTON TEXT
        if currnum == min(self.numbers):            
            self.numbtn[mynum].config(state="disabled")    # DISABLE BUTTON
            self.numbers.remove(currnum)                   # REMOVE FOR NEW MINIMUM

NUMapp()
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125