I'm trying to add a lot of text onto a window in tkinter, and I want them to be in a row. Can I do it in a simpler way than what I am doing because it's really annoying to keep adding labels and changing the row every time. ("bingbong" is just an example, instead of those bingbongs I will have different passwords in a row.)
from tkinter import *
from functools import partial
def validateLogin(password):
print(password.get())
if password.get() == "test":
newWindow = Tk()
newWindow.geometry('1800x800')
newWindow.title("Passwords")
tkWindow.destroy()
Label(newWindow, text="bingbong").grid(row=1, column=0)
Label(newWindow, text="bingbong").grid(row=2, column=0)
Label(newWindow, text="bingbong").grid(row=3, column=0)
Label(newWindow, text="bingbong").grid(row=4, column=0)
Label(newWindow, text="bingbong").grid(row=5, column=0)
Label(newWindow, text="bingbong").grid(row=6, column=0)
Label(newWindow, text="bingbong").grid(row=7, column=0)
Label(newWindow, text="bingbong").grid(row=8, column=0)
if password.get() != "test":
Label(tkWindow, text="Wrong password!", fg='red').grid(row=5, column=2)
#window
tkWindow = Tk()
tkWindow.geometry('250x100')
tkWindow.title('Passwords')
#password label and password entry box
passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)
password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*').grid(row=1, column=2)
validateLogin = partial(validateLogin, password)
#login button
loginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4, column=2)
tkWindow.mainloop()
Use a loop to create them instead of creating them manually one at a time
from tkinter import *
from functools import partial
words = ["foo", "bar", "baz", "bingbong", "foobar", "foobaz", "foofoo", "barbar"]
def validateLogin(password):
print(password.get())
if password.get() == "test":
newWindow = Tk()
newWindow.geometry('1800x800')
newWindow.title("Passwords")
tkWindow.destroy()
for index, word in enumerate(words):
Label(newWindow, text=word).grid(row=index, column=0)
if password.get() != "test":
Label(tkWindow, text="Wrong password!", fg='red').grid(row=5, column=2)
#window
tkWindow = Tk()
tkWindow.geometry('250x100')
tkWindow.title('Passwords')
#password label and password entry box
passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)
password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*').grid(row=1, column=2)
validateLogin = partial(validateLogin, password)
#login button
loginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4, column=2)
tkWindow.mainloop()
Edit: use enumerate function to access the string value (password in this case) and index simultaneously