skip to Main Content

I am working on a project called "Flash card". Does anyone know how to share the same object between more than two functions? It seems setting the variable as global is not the answer. May I know why it isn’t? In my case, I want the same "dict2" to be used in Known(), Switch_Word() and Reset(), as it will be refilled the original data when Reset() is pressed. Then, it will be passed to Switch_Word() and Known() for further processing. I found out that there are different "dict2" when printing their ID.

from tkinter import *
import pandas
import random
import copy

BACKGROUND_COLOR = "#B1DDC6"
dataframe = pandas.read_csv(r"C:UsersUserDesktopPython Visual Studio Code100DaysDay31flash-card-project-startdatafrench_words.csv")
global dict
dict = dataframe.to_dict(orient="records")
print(len(dict))
global dict2 
dict2 = copy.deepcopy(dict)
Current_Vocab = {}

def Switch_to_English():
    canvas.itemconfig("Image",image=bg_image)
    canvas.itemconfig("title",text="English",fill="white")
    Current_Vocab_English = Current_Vocab["English"]
    canvas.itemconfig("word",text=f"{Current_Vocab_English}",fill="white")

def Switch_Word():
    global Current_Vocab
    canvas.itemconfig("Image",image = ft_image)
    Current_Vocab = random.choice(dict2)
    Current_Vocab_French = Current_Vocab["French"]
    canvas.itemconfig("title",text="French",fill = "black")
    canvas.itemconfig("word",text=f"{Current_Vocab_French}", fill = "black")
    window.after(3000,Switch_to_English)

def Known():
    Switch_Word()
    dict2.remove(Current_Vocab)
    print(len(dict2))
    print(id(dict2))

def Reset():
    dict2 = copy.deepcopy(dict)
    print(len(dict2))
    print(id(dict2))
    Switch_Word()

window = Tk()
window.title("Flashy")
window.config(padx=50, pady=50, bg=BACKGROUND_COLOR)

ft_image = PhotoImage(file= r"C:UsersUserDesktopPython Visual Studio Code100DaysDay31flash-card-project-startimagescard_front.png")
bg_image = PhotoImage(file= r"C:UsersUserDesktopPython Visual Studio Code100DaysDay31flash-card-project-startimagescard_back.png")
tick_image = PhotoImage(file= r"C:UsersUserDesktopPython Visual Studio Code100DaysDay31flash-card-project-startimagesright.png")
cross_image = PhotoImage(file= r"C:UsersUserDesktopPython Visual Studio Code100DaysDay31flash-card-project-startimageswrong.png")
Rev = []

flip_timer = window.after(3000, func=Switch_Word)
canvas = Canvas(width=800, height = 526, highlightthickness=0)
canvas.create_image(400, 253, image=ft_image, tags = "Image")
canvas.create_text(400, 158, text="Title", font = ("Arial",40,"italic"), tags = "title")
canvas.create_text(400, 263, text="Word", font = ("Arial",60,"italic"), tags = "word")
canvas.config(bg=BACKGROUND_COLOR,highlightthickness=0)
canvas.grid(row=0,column=1)

tick_label = Label(image=tick_image, bg = BACKGROUND_COLOR, highlightthickness= 0)
tick_btn = Button(window, image=tick_image, command=Known)
tick_btn.grid(row=1,column=2)

cross_label = Label(image=cross_image, bg=BACKGROUND_COLOR, highlightthickness=0)
cross_btn = Button(window, image=cross_image, command=Switch_Word)
cross_btn.grid(row=1,column=0)

Reset_Button = Button(text="Reset", command = Reset)
Reset_Button.grid(row=0,column=2)


window.mainloop()

2

Answers


  1. use a lambda function, e.g.:
    cross_btn = Button(window, image=cross_image, command=lambda:Switch_Word(dict_2))

    This just needs to be replicated for all applicable buttons. The lambda allows for you to pass arguments to the command, rather than just the function. You will of course have to modify your functions to accept an argument.

    Login or Signup to reply.
  2. You can use functools.partial

    Pass your common dictionary in every function where you need

    def your_function(common_dict):
        # Your code here 
    

    And in command use
    command=partial(your_function, common_dict)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search