Reputation: 1
Here is some code I wrote, program to contain words.
There's a button supposedly to remove duplicate words, but it won't work.
Please, why is it so?
The problem:
import tkinter as tk
def remove_dups(x):
list(dict.fromkeys(x))
update_display()
def update_display():
words_show.config(text="\n".join(words_list))
words_list = ["Banana", "Banana", "Bnanana"]
root = tk.Tk()
root.title("Word showoff")
words_show = tk.Label(root, text="\n".join(words_list), justify="left", width=30)
words_show.pack()
remove_dups_button = tk.Button(root, text="Remove dups", command=remove_dups(words_list))
remove_dups_button.pack()
root.mainloop()
Full code:
import tkinter as tk
def add_word():
word = entry.get()
if word:
words_list.append(word)
entry.delete(0, tk.END)
update_display()
def remove_dups(x):
list(dict.fromkeys(x))
update_display()
def update_display():
words_show.config(text="\n".join(words_list))
def close_program():
root.quit()
words_list = ["Banana", "Banana", "Bnanana"]
#####
root = tk.Tk()
root.title("Word showoff")
entry = tk.Entry(root, width=30)
entry.pack()
add_button = tk.Button(root, text="Add Word", command=add_word)
add_button.pack()
words_show = tk.Label(root, text="\n".join(words_list), justify="left", width=30)
words_show.pack()
remove_dups_button = tk.Button(root, text="Remove dups", command=remove_dups(words_list))
remove_dups_button.pack()
close_button = tk.Button(root, text="Close", command=close_program)
close_button.pack()
#####
root.mainloop()
I made a function that would update the words list removing the duplicates by turning elements into dictionary keys and then back into a list, and as a separate line of code it worked, but when I tried to make it into a function and call using a Tkinter button it wouldn't do nothing.
Upvotes: -1
Views: 63
Reputation: 47085
It is because remove_dups(x)
doesn't update the passed list x
, so nothing will be updated.
Just set the unique keys back to the passed list as below:
def remove_dups(x):
# set the unique keys back to the list
x[:] = list(dict.fromkeys(x))
update_display()
Also command=remove_dups(words_list)
should be command=lambda: remove_dups(words_list)
.
Upvotes: 1
Reputation: 549
import tkinter as tk
def add_word():
word = entry.get()
if word:
words_list.append(word)
entry.delete(0, tk.END)
update_display()
def remove_dups():
global words_list
words_list = list(dict.fromkeys(words_list))
update_display()
def update_display():
words_show.config(text="\n".join(words_list))
def close_program():
root.quit()
words_list = ["Banana", "Banana", "Bnanana"]
#####
root = tk.Tk()
root.title("Word showoff")
entry = tk.Entry(root, width=30)
entry.pack()
add_button = tk.Button(root, text="Add Word", command=add_word)
add_button.pack()
words_show = tk.Label(root, text="\n".join(words_list), justify="left", width=30)
words_show.pack()
# Fix: Use lambda to delay function execution
remove_dups_button = tk.Button(root, text="Remove dups", command=lambda: remove_dups())
remove_dups_button.pack()
close_button = tk.Button(root, text="Close", command=close_program)
close_button.pack()
#####
root.mainloop()
I think the issue is with how you're calling the remove_dups
function.
To fix it, you could pass the function reference (without calling it) using a lambda function or a partial function from the functools
module.
Upvotes: 0
Reputation: 1
import tkinter as tk
def add_word():
word = entry.get()
if word:
words_list.append(word)
entry.delete(0, tk.END)
update_display()
def remove_dups(x):
global words_list # Ensure we modify the global variable
words_list = list(dict.fromkeys(x)) # Remove duplicates and update the list
update_display() # Refresh the UI
def update_display():
words_show.config(text="\n".join(words_list))
def close_program():
root.quit()
words_list = ["Banana", "Banana", "Bnanana"]
root = tk.Tk()
root.title("Word showoff")
entry = tk.Entry(root, width=30)
entry.pack()
add_button = tk.Button(root, text="Add Word", command=add_word)
add_button.pack()
words_show = tk.Label(root, text="\n".join(words_list), justify="left", width=30)
words_show.pack()
remove_dups_button = tk.Button(root, text="Remove dups", command=lambda: remove_dups(words_list))
remove_dups_button.pack()
close_button = tk.Button(root, text="Close", command=close_program)
close_button.pack()
root.mainloop()
The remove_dups function does not modify words_list In your remove_dups function, you create a new list but do not assign it back to words_list. This means words_list remains unchanged.
The Button Calls the Function Immediately Instead of on Click You're calling remove_dups(words_list) immediately when the script runs, instead of passing a reference to the function.
Fix: Use lambda to Delay Execution
Upvotes: 0