Reputation: 1
So I want to make a programm with Tkinter where you type in some stuff (like Bananas,Apple,Watermelone) and then I want to put those things in a list. Can I somehow seperate the Input I get with the .get function?
from tkinter import *
from random import *
root = Tk()
Eingabe=Entry(root)
l = list()
eget=l.append(Eingabe.get)
def rNdm():
x = randint(0, 3)
for r in l:
if l.index(r) == x:
print(r)
myLabel=Label(root, text=r)
myLabel.pack()
Eingabe=Entry(root)
Eingabe.pack()
myButton = Button(root, text="Rndm", command=rNdm)
myButton.pack()
root.mainloop()
This is the code I have until now, but it only prints out stuff like that. This is the Tkinter Window
(And if you wonder why the code looks so unsorted, I'm only a beginner)
Upvotes: 0
Views: 191
Reputation: 142651
You forgot ()
in get()
.
get()
gives normal string so you can use .split(",")
to convert string "Bananas,Apple,Watermelone"
to list ["Bananas", "Apple", "Watermelone"]
But you have other problem: Entry()
doesn't work like input()
- it doesn't wait for your text. It only informs tkinter
what widget it has to display in window. And mainloop()
starts everything. It displays window with widgets. So your code tries to get text from Entry
before you can even see it on screen - so it can get only empty string. You have to run it in your function rNdm
which you run using Button
.
And the same is in most GUIs - not only in Python.
BTW:
To get random element from list you can use random.choice(your_list)
Minimal working code:
import tkinter as tk # PEP8: `import *` is not preferred
import random # PEP8: `import *` is not preferred
# --- functions ---
def rNdm():
global l # inform function to use external variable when you will assign value (`=`) to this variable
text = eingabe.get()
l = text.split(",")
random_item = random.choice(l)
my_label['text'] = random_item # replace text in existing `Label`
print('l:', l)
print('random_item:', random_item)
# --- main ---
l = list() # it is global variable
root = tk.Tk()
eingabe = tk.Entry(root) # PEP8: `lower_case_names` for variables
eingabe.pack()
my_button = tk.Button(root, text="Rndm", command=rNdm)
my_button.pack()
my_label = tk.Label(root) # without text
my_label.pack()
root.mainloop()
EDIT:
Version which keep last 5 words on list and display these words in one Label
import tkinter as tk # PEP8: `import *` is not preferred
import random # PEP8: `import *` is not preferred
# --- functions ---
def rNdm():
global all_words # inform function to use external variable when you will assign value (`=`) to this variable
text = eingabe.get()
items = text.split(",")
random_item = random.choice(items)
all_words.append(random_item)
all_words = all_words[-5:] # keep only last 5 words
my_label['text'] = "\n".join(all_words) # replace text in existing `Label`
print('items:', items)
print('random_item:', random_item)
print('all_words:', all_words)
# --- main ---
all_words = list() # it is global variable
root = tk.Tk()
eingabe = tk.Entry(root) # PEP8: `lower_case_names` for variables
eingabe.pack()
eingabe.insert('end', 'Bananas,Apple,Watermelone')
my_button = tk.Button(root, text="Rndm", command=rNdm)
my_button.pack()
my_label = tk.Label(root) # without text
my_label.pack()
root.mainloop()
Version which use Listbox
+Scrollbar
to display all values.
import tkinter as tk # PEP8: `import *` is not preferred
import random # PEP8: `import *` is not preferred
# --- functions ---
def rNdm():
global all_words # inform function to use external variable when you will assign value (`=`) to this variable
text = eingabe.get()
items = text.split(",")
random_item = random.choice(items)
my_listbox.insert('end', random_item)
all_words.append(random_item)
print('items:', items)
print('random_item:', random_item)
print('all_words:', all_words)
# --- main ---
all_words = list() # it is global variable
root = tk.Tk()
eingabe = tk.Entry(root) # PEP8: `lower_case_names` for variables
eingabe.pack(fill='x')
eingabe.insert('end', 'Bananas,Apple,Watermelone')
my_button = tk.Button(root, text="Rndm", command=rNdm)
my_button.pack()
# --- Listbox + Scrollbar (in Frame to organize it) ---
results = tk.Frame(root)
results.pack(fill='both', expand=True)
my_listbox = tk.Listbox(results) # without text
my_listbox.pack(side='left', fill='both', expand=True)
my_scrollbar = tk.Scrollbar(results) # without text
my_scrollbar.pack(side='right', fill='y')
# connect scrollbar with listbox
my_scrollbar['command'] = my_listbox.yview
my_listbox['yscrollcommand'] = my_scrollbar.set
# ---
root.mainloop()
Upvotes: 1