im currently trying to create a button widget using Tkiner and would like to know how to get a number from that button when i click on it. I have tried lots of methods and ideas from different people and i still haven't got the right bit of code. any help is very helpful.

def createEight(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    8    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=2, column=1, sticky=N+E+S+W)

Check out this code which does 8 buttons and handles them in one function.

Any help?

#!/usr/local/bin/Python
import Tkinter

def doButton(buttonName):
    global but
    print buttonName
    
class SimpleCallback:
    def __init__(self, callback, *firstArgs):
        self.__callback = callback
        self.__firstArgs = firstArgs

    def __call__(self, *args):
        return self.__callback (*(self.__firstArgs + args))


if __name__=="__main__":
    root = Tkinter.Tk()
##    root.protocol("WM_DELETE_WINDOW", quit)

    button=dict()
    for name in range(8):
        callback = SimpleCallback(doButton, name)
        but=Tkinter.Button(root, text=str(name), command=callback)
        button[name]=but
        but.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)
    root.mainloop()

thank you very much. greatly appreciated :)

Wrong thread message.

Here I used a basic Tkinter class template to test your method createEight(self) and gave the button a command ...

# Tkinter class template to test apps

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Tkinter basic class")
        # use width x height + x_offset + y_offset (no spaces!)
        self.geometry("300x150+50+50")
        self['bg'] = 'green'
        self.createEight()

    def createEight(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.button8 = tk.Button(self, text="    8    ",
            font=("Arial", 12), bg="white", fg="blue",
            cursor="crosshair", command=lambda: self.click('8'))
        self.button8.grid(row=2, column=1, sticky='nesw')
        
    def click(self, val):
        s = "Button " + val + " clicked"
        # show result in title
        self.title(s)


app = MyApp()
app.mainloop()

Nice. The feed pack of second click is however missing so let's demonstrate the after effect with your receipt of lambda to hide the constant parameter.

# Tkinter class template to test apps

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title(title)
        # use width x height + x_offset + y_offset (no spaces!)
        self.geometry("300x150+50+50")
        self['bg'] = 'green'
        self.createEight()

    def createEight(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.button8 = tk.Button(self, text="    8    ",
            font=("Arial", 12), bg="white", fg="blue",
            cursor="crosshair", command=lambda: self.click('8'))
        self.button8.grid(row=2, column=1, sticky='nesw')
        
    def click(self, val):
        s = "Button " + val + " clicked"
        # show result in title
        self.title(s)
        self.button8.after(800,lambda:self.title(title)) ## put title  after 8 s


title="Tkinter basic class"
app = MyApp()
app.mainloop()

Mark solved or something still unclear?

i came up with a new idea to make an entry screen. wondering if i could change my code to make the button i clicked to appear in the entry screen from my code. thanks again for the help.

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.