亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我添加滾動條,所有標簽都會消失

如果我添加滾動條,所有標簽都會消失

白豬掌柜的 2023-06-27 17:36:40
在我的程序中,我想添加很多標簽,并且要查看所有標簽,它們需要滾動條。如果我在代碼中添加滾動條,那么所有標簽都會消失。我對長代碼表示歉意,由于某種原因,如果沒有所有額外的代碼,我就無法讓它工作。沒有滾動條的代碼:from tkinter import *from functools import partialwords = ["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)#windowtkWindow = Tk()tkWindow.geometry('250x100')tkWindow.title('Passwords')#password label and password entry boxpasswordLabel = 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 buttonloginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4, column=2)tkWindow.mainloop()帶滾動條的代碼:from tkinter import *from functools import partialwords = ["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)
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

我在這里看到兩個問題,第一個問題是您嘗試用于.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 中的窗口/框架上工作。這意味著修復原始問題后,標簽和滾動條現在都會加載,但滾動條不會執行任何操作。

查看完整回答
反對 回復 2023-06-27
  • 1 回答
  • 0 關注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號