python - 如果我添加滚动条,所有标签都会消失
问题描述
在我的程序中,我想添加很多标签,并且要查看它们需要滚动条。如果我在代码中添加滚动条,那么所有标签都会消失。我为长代码道歉,由于某种原因,如果没有所有额外的代码,我就无法让它工作。
没有滚动条的代码:
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)
#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()
解决方案
我在这里看到了两个问题,第一个问题是您尝试将其.grid
用于显示文本的标签,并.pack
用于滚动条。您不能在同一个窗口中同时.grid
使用两者。.pack
.grid(row=index, column=0)
这是我通过替换来纠正此问题的代码.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()
第二个问题是滚动条实际上没用。我不相信滚动条本身可以在 tkinter 中的窗口/框架上工作。这意味着在修复了原始问题后,标签和滚动条现在都将加载,但滚动条将什么也不做。 这是我从谷歌找到的一个快速教程,它可能会帮助你让滚动条工作
推荐阅读
- swift - 在 Swift 中使用暗模式更改更改 macOS Dock 图标
- javascript - Jquery单击事件的错误并已检查
- matlab - Textscan 和 Regexp 单元格数据布局
- php - PHP 7.4 上的预加载和 JIT(将在 PHP 8 上发布)有什么区别
- laravel - 我如何在 laravel 中使用 index_number 和 date_of_birth 进行前端登录,用户必须先注册
- python - OpenSSL FIPS_mode_set 在 Python 密码库中不起作用
- c++ - 如何从文件中读取数字并在数组中使用它们?
- python - Six.text_type 和 text.decode('utf8') 一样吗?
- python - FLASK-RQ2 如何使用作业装饰器调用函数
- java - MagicDraw 是否有 Python 接口或 API 包装器?