Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do i get / How do i remove the ugly curly brackets? (Python3.9/Tkinter)

Hi everyone!

I don’t really know anything about coding (as you can probably tell by my code), and I’ve somehow put together this random-picker with the help of way more YouTube videos than I’d like to admit to!

Now, I’m getting a very weird output and I have no clue why it is like this. Can anyone help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In “liste.txt” are just multiple lines of “normal” text. (as I show in the video)

The output I’m getting: YouTube

I want exactly this output, just without the curly brackets ("{}") and without the weird Ä/Ö/Ü formatting.

If you see anything else horribly wrong with my code, please let me know. I want to improve.

Thanks & Greetings from Switzerland!!

import random
from tkinter import *
import pygame


win=Tk()
win.geometry("1200x600")
win.title("RandomPickerSLF - by atefxf")


with open("liste.txt", "r") as f:
   liste = f.readlines()


Label(win, text="Wie viele Themen möchtest du haben?", font=('Calibri 12')).pack(pady=60)
a=Entry(win, width=35)
a.pack()
a.insert(0, "5")


pygame.mixer.init()


def anzeigen():
   t1=int(a.get())
   choice = random.choices(liste, k=(t1))
   label1.config(text=choice, font=('Calibri 13'))
   pygame.mixer.music.load("click1.wav")
   pygame.mixer.music.set_volume(0.3)
   pygame.mixer.music.play(loops=0)


label = Label(win, text="Deine Themen : ", font=('Calibri 15'))
label.pack(pady=10)


label1 = Label(win, text=" ", font=('Calibri 15'))
label1.pack(pady=20)


Button(win, text="Lasse dir deine Themen anzeigen!", command=anzeigen).pack(pady=90)


win.mainloop()

>Solution :

It is because you pass a list of strings with trailing newline to text option of Label.

Either strip out the newline:

choice = [x.strip() for x in random.choices(liste, k=t1)]

or joining the strings into one string:

choice = ''.join(random.choices(liste, k=t1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading