Python Forum
[Tkinter] Button click problem using OOP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button click problem using OOP
#1
Can anyone help me with a problem illustrated in the code below. I am instantiating three tkinter buttons and saving the objects in a list which is I think a fairly standard way of doing things. The buttons can then be managed, colour change, text etc by referencing them via their index in the list. But if a button is clicked it seems impossible to detect which button it was. The only button object that is "active", if you want to call it that, is the last one that was instantiated in the For loop. Which ever button is clicked, it is that object that handles the click event. Is there a way of detecting which button is clicked when they have been instantiated this way. Thank you for sharing your expertise.

JohnB

import tkinter as tk
root=tk.Tk()

class Butman(tk.Frame):
    def __init__(self,MyNumber):
        self.MyNumber = MyNumber
        tk.Frame.__init__(self)
        
    def SetCol(self,col):
        self.but.config(bg = col)
        
    def SetText(self,Text):
        self.but.config(text = Text)



def DoSomething():
    print ("something done")
    
        
frame2 = tk.Frame(bg="grey", height = 720 ,width = 1280)
frame2.pack()
ButObj = []
for x in range(0,3):
    B = Butman(x)
    B.but=tk.Button(frame2, state="normal", text="sometext",compound= tk.CENTER,font=('comicsans', 
    16),relief=tk.FLAT,command = lambda: DoSomething())
    B.but.place(relx=x/4, rely=x/4, relwidth=0.18, relheight=0.18)
    ButObj.append(B)
    
ButObj[1].SetCol("green")
B.SetCol("red")
Reply


Messages In This Thread
Button click problem using OOP - by JohnB - Oct-18-2020, 07:41 PM
RE: Button click problem using OOP - by deanhystad - Oct-19-2020, 03:38 AM
RE: Button click problem using OOP - by joe_momma - Oct-19-2020, 04:59 AM
RE: Button click problem using OOP - by JohnB - Oct-20-2020, 11:05 AM
RE: Button click problem using OOP - by deanhystad - Oct-20-2020, 04:30 PM
RE: Button click problem using OOP - by JohnB - Oct-21-2020, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Modify Class on Button Click KDog 4 1,276 May-11-2021, 08:43 PM
Last Post: KDog
  tkinter python button position problem Nick_tkinter 3 708 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  tkinter | Button color text on Click Maryan 2 737 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Closing window on button click not working kenwatts275 4 779 May-03-2020, 01:59 PM
Last Post: deanhystad
  Problem about image and button scotesse 5 948 Apr-27-2020, 10:09 AM
Last Post: scotesse
  Problem with Submit button Tkinter Reldaing 2 1,247 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 4,498 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [PyQt] Problem how to click a button inside a group box? mart79 2 1,197 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 2,079 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 1,637 May-12-2019, 08:17 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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