添加大量标签的更好方法
Better way to add a lot of labels
我想在 tkinter 的 window 上添加很多文本,我希望它们排成一行。我可以用比我现在做的更简单的方式来做吗,因为每次都不断添加标签和更改行真的很烦人。 (“bingbong”只是一个例子,我将连续使用不同的密码而不是那些 bingbongs。)
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()
使用循环创建它们,而不是一次一个地手动创建它们
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()
编辑:使用枚举函数同时访问字符串值(本例中为密码)和索引
我想在 tkinter 的 window 上添加很多文本,我希望它们排成一行。我可以用比我现在做的更简单的方式来做吗,因为每次都不断添加标签和更改行真的很烦人。 (“bingbong”只是一个例子,我将连续使用不同的密码而不是那些 bingbongs。)
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()
使用循环创建它们,而不是一次一个地手动创建它们
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()
编辑:使用枚举函数同时访问字符串值(本例中为密码)和索引