I'm trying to select random names from a list to display on a textbox in a tkinter GUI, with a button and need new draws to display in tkinter script each time I click the button.
Here's my code so far:
from tkinter import *
from random import sample
root = Tk()
root.title('Name Picker')
root.iconbitmap('c:/myguipython/table.ico')
root.geometry("400x400")
def pick():
# 5 entries
entries = ["1st name","2nd name","3rd name","4th name","5th name"]
# convert to a set
entries_set = set(entries)
# convert back to a list
unique_entries = list(entries_set)
number_of_items = 2
rando = sample(unique_entries, number_of_items)
rando1 = sample(unique_entries, number_of_items)
my_text = Text(root, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=10)
if len(my_text.get(1.0, END)) != 0:
my_text.delete(1.0, END)
my_text.insert(INSERT, rando rando1)
my_text.pack(expand= 1, fill= BOTH)
topLabel = Label(root, text="Picked Names", font=("Helvetica", 24))
topLabel.pack(pady=20)
winButton = Button(root,
text="pick names",
font=("Helvetica", 24), command=pick)
winButton.pack(pady=20)
root.mainloop()
I need several subsets of random draws (I'm using rando and rando1 values as example above).
When I click the button the 1st time it does display the names strings as expected, but when I click again nothing changes (the names remain the same).
What I need is for new draws to display each time I click the button. I thought of clearing the textbox if not empty and populating back when clicking again on empty textbox (with the delete and insert statements above). But that's not working.
After some research I found this snippet:
# Import package and it's modules
from tkinter import *
# text_update function
def text_updation(language):
text.delete(0, END)
text.insert(0, language)
# create root window
root = Tk()
# root window title and dimension
root.title("GeekForGeeks")
# Set geometry (widthxheight)
root.geometry('400x400')
# Entry Box
text = Entry(root, width=30, bg='White')
text.pack(pady=10)
# create buttons
button_dict = {}
words = ["Python", "Java", "R", "JavaScript"]
for lang in words:
# pass each button's text to a function
def action(x = lang):
return text_updation(x)
# create the buttons
button_dict[lang] = Button(root, text = lang,
command = action)
button_dict[lang].pack(pady=10)
# Execute Tkinter
root.mainloop()
It seems it's close to what I need but the dictionary use seems incompatible with my conversion to a set and then back to a list and then the sampling before the displaying in the textbox of my code.
How could I use the given snippet to solve this issue? Or lese if it's not possible with this snippet, what other other solution of insight to look for would you suggest? Your help is appreciated.
CodePudding user response:
In your code you are calling the widget constructor for the Text widget every time you press the button. What you should do is create the widget outside of the function and use the function just to update the widget's contents. There is also quite a bit of erroneous code as well.
For example:
from tkinter import *
from random import sample
root = Tk()
root.title('Name Picker')
root.iconbitmap('c:/myguipython/table.ico')
root.geometry("400x400")
def pick():
entries = ["1st name","2nd name","3rd name","4th name","5th name"]
number_of_items = 2
rando = sample(entries, number_of_items)
rando1 = sample(entries, number_of_items)
my_text.delete(1.0, END)
my_text.insert(INSERT, rando rando1)
topLabel = Label(root, text="Picked Names", font=("Helvetica", 24))
topLabel.pack(pady=20)
winButton = Button(root, text="pick names", font=("Helvetica", 24), command=pick)
winButton.pack(pady=20)
my_text = Text(root, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=10)
root.mainloop()