Neil
Neil

Reputation: 796

Keeping track of label bindings

This is a simplified version of a Twitter app I'm working on. Using this setup, how can I keep track of which "Retweet" label the user clicked?

I want the retweet function to return with the "Id" of either "Tweet1" "Tweet2" or "Tweet3", depending on which "Retweet" label the user clicks on.

Any advice would be appreciated.

import tkinter as tk
from tkinter import ttk


class GetTweet:

    def __init__(self):

        self.tweet_list = []
        self.tweet1 = {"id": 1, "text": "tweet1"}
        self.tweet2 = {"id": 2, "text": "tweet2"}
        self.tweet3 = {"id": 3, "text": "tweet3"}
        self.tweet_list.extend([self.tweet1, self.tweet2, self.tweet3])


class Widgets:

    def __init__(self):

        self.label_list = []
        self.tweet_text_var = []

        x = 0
        for n in range(0, 6, 2):

            self.tweet_text_var.append(tk.StringVar())
            self.tweet_text = ttk.Label(root, textvariable=self.tweet_text_var[x])
            self.tweet_text.grid(column=0, row=n)
            self.label_list.append(object)
            self.label_list[x] = ttk.Label(text="Retweet", cursor="hand2")
            self.label_list[x].grid(column=0, row=n+1)
            self.label_list[x].bind("<Button-1>", retweet)

            x += 1


class PopulateVariables:

    def __init__(self):
        for n in range(0, 3):
            widgets.tweet_text_var[n].set(get_tweet.tweet_list[n].get("text"))


def retweet(event):
    print(get_tweet.tweet_list)


root = tk.Tk()
widgets = Widgets()
get_tweet = GetTweet()
populate_variable = PopulateVariables()
root.mainloop()

Upvotes: 0

Views: 71

Answers (1)

Neil
Neil

Reputation: 796

I just figured this out:

By changing the Bind to :

self.label_list[x].bind("<Button-1>", lambda event, ref=x: retweet(ref))

I can get the relevant ID from the "retweet" function by calling :

def retweet(ref):
    print(get_tweet.tweet_list[ref].get("id"))

Here is the whole code:

import tkinter as tk
from tkinter import ttk


class GetTweet:

    def __init__(self):

        self.tweet_list = []
        self.tweet1 = {"id": 1, "text": "tweet1"}
        self.tweet2 = {"id": 2, "text": "tweet2"}
        self.tweet3 = {"id": 3, "text": "tweet3"}
        self.tweet_list.extend([self.tweet1, self.tweet2, self.tweet3])


class Widgets:

    def __init__(self):

        self.label_list = []
        self.tweet_text_var = []

        x = 0
        for n in range(0, 6, 2):

            self.tweet_text_var.append(tk.StringVar())
            self.tweet_text = ttk.Label(root, 
                                        textvariable=self.tweet_text_var[x])
            self.tweet_text.grid(column=0, row=n)
            self.label_list.append(object)
            self.label_list[x] = ttk.Label(text="Retweet", cursor="hand2")
            self.label_list[x].grid(column=0, row=n+1)
            self.label_list[x].bind("<Button-1>", lambda event, ref=x: 
                                    retweet(ref))

            x += 1


class PopulateVariables:

    def __init__(self):
        for n in range(0, 3):
          widgets.tweet_text_var[n].set(get_tweet.tweet_list[n].get("text"))


def retweet(ref):
    print(get_tweet.tweet_list[ref].get("id"))



root = tk.Tk()
widgets = Widgets()
get_tweet = GetTweet()
populate_variable = PopulateVariables()
root.mainloop()

Upvotes: 1

Related Questions