0

So I have a boggle game that I wrote on tkinter, and part of the GUI is a list of all words the user found during the game. I put this list in a StringVar on a label, but my problem is that I want the label to expand whenever the StringVar is exceeding from the label boundaries. For example, if there is a space in the label for 10 words and the player found 10 words, so the next word will exceed from the label boundaries, so I need to find a way to expand the label dynamically according to the current space given.

This is the game:

enter image description here

As you can see in the right bottom there is a "Words Found:" label with black border and I want to make it expand by itself whenever a new word is insert and there is not enough space for the word so that all the words will be shown.

I tried to write it with scrollbar but with no success.

This is my current code for this specific label:

def __init_words_found(self):
    self.general_label = tk.Label(self.root,
                                  text='Words Found:',
                                  font="Helvetica 10 bold")
    self.general_label.place(x=460, y=220)

    self.__words_found_frame = tk.Frame(self.root, width=160, height=20,
                                        relief="solid", bd=1)
    self.__words_found_frame.place(x=460, y=255)
    self.__words_found_var.set("" + "\n".join(self.words_found))

    self.__words_label = tk.Label(self.__words_found_frame,
                                  textvariable=self.__words_found_var,
                                  width=3, height=3, font="Helvetica 9")
    self.__words_label.pack()
  • Lets say you have `x=tk.StringVar(value='')` and you set it in a function with `x.set('my_word')`. You could easily do `x.set(x.get()+'\n'+'my_new_word')` where `+'\n'` will create a new line. [Also see](https://stackoverflow.com/q/12169839/13629335) – Thingamabobs Jan 06 '22 at 16:58
  • I have a "\n" already but it is not expanding the label, the words just exceeds from the label – PythonAddict Jan 06 '22 at 17:00
  • Oh, my bad. Try `self.__words_label.pack(fill=tk.BOTH,expand=True)` – Thingamabobs Jan 06 '22 at 17:02
  • it seems to have no effect :\ – PythonAddict Jan 06 '22 at 17:14
  • `self.__words_found_var.set("" + "\n".join(self.words_found))` should be `self.__words_found_var.set(self.__words_found_var.get() + "\n".join(self.words_found))` like I said. Do you do this? – Thingamabobs Jan 06 '22 at 17:45
  • yes I did it but it didn't solve it – PythonAddict Jan 06 '22 at 17:53
  • If I run your code with the neccesary changes to create a [mre] and the methods I mention it works, beside a non resizing `self.__words_found_frame` because of the geometry manager `place`. So please create a [mre] – Thingamabobs Jan 06 '22 at 18:18
  • Did you try removing `width` and `height` options when creating `self.__words_label`? – acw1668 Jan 07 '22 at 00:33

0 Answers0