Hey people, i was just wondering if anyone knew the code to make a tkinter button...

def createExample(self):
        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=" Example", font=("Arial", 12), bg="white", fg="magenta", cursor="crosshair")
        self.Button.grid(row=1, column=0, sticky=N+E+S+W)

...and i wanted to know the code that you can click on that button and the value of that button can apper in an entry screen. For example if you made a button that was 1, if you click on it, it displays the number 1 in an entry screen.

thanks for any help


This is one way to do it ...

# create a list of buttons
# show click values in an entry

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

def click(val):
    s = "Button " + val + " clicked"
    entry.insert('end', val)

root = tk.Tk()

# create the list of button objects
button_list = [
tk.Button(root, text=" 1 ", bg='red', command=lambda: click('1')),
tk.Button(root, text=" 2 ", bg='green', command=lambda: click('2')), 
tk.Button(root, text=" 3 ", bg='white', command=lambda: click('3'))

# show buttons
for button in button_list:
    button.pack(side='left', padx=10)

# create an entry to show button values
entry = tk.Entry(root)
entry.pack(padx=3, pady=3)


i have typed up 16 buttons using that code in the trend and was wondering how to change it so i could use my code to work.



This article has been dead for over six months. Start a new discussion instead.