0

In my program I want to add a lot of labels, and to see them all they require a scrollbar. If i add a scrollbar to my code then all labels disappear. I apologize for the long codes, I just couldn't get it work without all the extra code for some reason.

Code without the scrollbar:

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()

Code with the scrollbar:

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)

#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()
Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
Axsor
  • 69
  • 6
  • 1
    The labels "disappear" because your program is encountering errors (see the console when you run it), stemming from the fact that you use `grid` for labels and `pack` for the scrollbar - you have to use the same method for placing everything in a frame – Tom Aug 27 '20 at 20:06
  • 1
    On top of this, your scrollbar is not doing anything at the moment; it is slightly more complicated to link the scrollbar to the Frame you want to scroll through (more than just placing it in the frame). But you can look at [this question](https://stackoverflow.com/q/16188420/13386979) (and likely others) for a working example – Tom Aug 27 '20 at 20:08
  • 2
    @Tom - regarding: you have to use the same method for placing everything in a frame ~ this is only half true. You just can't use `grid` and `pack` together. You can use `place` all you want. – OneMadGypsy Aug 28 '20 at 00:21

1 Answers1

2

There are two problems that I have seen here, the first problem is that you're trying to use .grid for the label displaying the text, and using .pack for the scrollbar. You can't use both .grid and .pack in the same window.

Here's the code where I corrected this problem by replacing .grid(row=index, column=0) with .pack(anchor = W)

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).pack(anchor = W)

    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()

The second problem is that the scrollbar is practically useless. I don't believe that scrollbars natively work on windows/frames in tkinter. This means that after fixing the original problem, both the Labels and scrollbar will now load, but the scrollbar will do nothing. Here's a quick tutorial I found from google that should probably help you to get the scrollbar to work

nose_gnome
  • 123
  • 5