在我的程序中,我想添加很多标签,并且要查看所有标签,它们需要滚动条。如果我在代码中添加滚动条,那么所有标签都会消失。我对长代码表示歉意,由于某种原因,如果没有所有额外的代码,我就无法让它工作。
没有滚动条的代码:
from tkinter import *
from functools import partial
words = ["test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test"]
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()
带滚动条的代码:
from tkinter import *
from functools import partial
words = ["test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test"]
def validateLogin(password):
print(password.get())
if password.get() == "test":
newWindow = Tk()
newWindow.geometry('1800x800')
newWindow.title("Passwords")
scrollbar = Scrollbar(newWindow)
scrollbar.pack(side=RIGHT, fill=Y)
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)
潇湘沐
相关分类