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?
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()
CodePudding user response:
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))