Python Forum
Problem with buttons
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with buttons
#6
Barrowman, yes, so a combination of both:
from tkinter import *
import tkinter as tk


def write_slogan(event):
    item = event.widget
    t = item.cget('text')
    print(t)


root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
btns = []

for k in range(0, 11, 1):
    btns.append(tk.Button(frame, text=str(k)))
    btns[k].pack(side=tk.LEFT)
    btns[k].bind("<Button-1>", write_slogan)

root.mainloop()
or cleaner:
import tkinter as tk

root = tk.Tk()

def write_slogan(event):
    btn = event.widget
    print(btn.cget('text'))

for i in range(10):
    b = tk.Button(root, text='{}'.format(i))
    b.grid(row=0, column=i)
    b.bind("<Button-1>", write_slogan)

root.mainloop()
Reply


Messages In This Thread
Problem with buttons - by Epilepsy - May-12-2018, 05:13 AM
RE: Problem with buttons - by Barrowman - May-12-2018, 03:38 PM
RE: Problem with buttons - by woooee - May-12-2018, 04:41 PM
RE: Problem with buttons - by Larz60+ - May-12-2018, 06:05 PM
RE: Problem with buttons - by Barrowman - May-12-2018, 09:23 PM
RE: Problem with buttons - by Larz60+ - May-12-2018, 11:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020