Reputation: 3964
I've been stuck on this for a long time and I've done lots of research but haven't found anything too helpful.
I am trying to place labels - made up of letters 'a' - 'j' in my frame. Every few seconds, I want the next letter to appear adjacent to the previous letter. But what happens is that every few seconds, the new letter replaces the previous letter - meaning I can't see the old letters anymore. I've tried tons of things and nothing is working. Can someone please help?
I've tried making a grid layout of all the labels within the frame within the parent frame but it's too complicated for me. Is there a way to fix this?
from tkinter import *
from ttk import *
import random
root = Tk()
# there are 10 tiles
tiles_letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
tiles_make_word = []
content = Frame(root)
frame = Frame(content, borderwidth=5, relief="ridge", width=200, height=300)
namelbl = Label(content, text="Enter words: ")
name = Entry(content)
ok = Button(content, text="Okay")
cancel = Button(content, text="Cancel")
content.grid(column=0, row=0)
frame.grid(column=0, row=0, columnspan=12, rowspan=12)
namelbl.grid(column=0, row=12)
name.grid(column=1, row=12, columnspan=2)
ok.grid(column=12, row=0)
cancel.grid(column=13, row=0)
def add_letter():
global i
i=0
if not tiles_letter:
return
rand = random.choice(tiles_letter)
tiles_make_word.append(rand)
tile_frame_column = Label(content, text=rand)
tile_frame_column.grid(column=i, row=0)
tiles_letter.remove(rand) # remove that tile from list of tiles
i=i+1
root.after(2000, add_letter)
root.after(2000, add_letter)
root.mainloop()
Upvotes: 0
Views: 1799
Reputation: 1269
The problem is the variable i
that you are using, not that it replaces the label each time, but that it places the label in the same place every time, effectively covering the older label up. This occurs due to you setting i=0
inside the function add_letter, you need to take it out and if using globals, global i
in both places, i.e.
global i
i=0
def add_letter():
global i
i+=1
However, this isn't ideal either as the space between the letters is quite big (due to the bottom widgets). I recommend using .place(x=..., y=...)
to overcome this, but this means you will have to use .place()
everywhere, so a big change in code would be needed.
Edit: here is re-worked code with .place()
used:
from tkinter import *
import random
root = Tk()
root.geometry("350x300")
# there are 10 tiles
tiles_letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
tiles_make_word = []
namelbl = Label(root, text="Enter words: ")
name = Entry(root)
ok = Button(root, text="Okay")
cancel = Button(root, text="Cancel")
namelbl.place(x=35, y=200)
name.place(x=110, y=200, width=100)
ok.place(x=200, y=100)
cancel.place(x=250, y=100)
global i
i=35
def add_letter():
global i
if not tiles_letter:
return
rand = random.choice(tiles_letter)
tiles_make_word.append(rand)
tile_frame_column = Label(root, text=rand)
tile_frame_column.place(x=i, y=50)
tiles_letter.remove(rand) # remove that tile from list of tiles
i+=10
root.after(2000, add_letter)
root.after(2000, add_letter)
Upvotes: 1