Python滚动条解决方法

3

在我的Python程序中,我倾向于将所有与文本相关的内容都使用标签进行处理。在为大约50个引用编写了一个非常长的标签后,现在我卡在了一个需要滚动条的多行标签的toplevel窗口上,而标签不会滚动。

我已经在窗口上放置了滚动条,但它无法滚动。是否有解决方法可以使我的“文本”标签上下滚动,或者我需要在toplevel窗口中放置不同的小部件?

        filewin = Toplevel(background="white")

        scrollbar=Scrollbar(filewin)
        scrollbar.pack(side=RIGHT, fill=Y)
        yscrollcommand=scrollbar.set 


        Label(filewin, text=". . .\n \
    Acidophilium \n\
            Wichlacz,P.L., Unz,R.F., Langworthy,T.A. 1986. Acidiphilium angustum sp. nov. Acidiphilium facilis sp. nov. and Acidiphilium vubrum sp. nov. : \n\
                Acidophilic Heterotrophic Bacteria Isolated from Acidic Coal Mine Drainage. Int J Syst Bacteriol 36:197-201. \n\
    Acinetobacter \n\
            Bouvet,P.J.M., Grimont,P.A.D. 1986. Taxonomy of the Genus Acinetobacter with the Recognition of Acinetobacter baumannii sp. nov. Acinetobacter haemolyticus sp. \n\
                nov. Acinetobacter johnsonii sp. nov. and Acinetobacter junii sp. nov. and Emended Descriptions of Acinetobacter calcoaceticus and Acinetobacter lwofii. \n\
                Int J Syst Bacteriol 36:228-240.",
        justify=LEFT, background="white", foreground="black", wraplength=1000).pack()
        filewin.title("Matrix References")

7
你可能应该提到你正在使用哪个GUI工具包。 - Jesse Aldridge
这并不是一个真正的Python问题。 - Daniel Roseman
最好包含更多的代码,比如你的导入,这样我们才能运行它。 - David Robinson
1
诚实地问问自己,如果你只提供所发布的严格信息,那么理解和解决问题的机会有多大。然后根据你的目标采取行动。 - joaquin
1个回答

3
您不能在标签中使用滚动条。
请改用文本
from Tkinter import *

root = Tk()

mytext = "Here_your very long text"

scrbar = Scrollbar(root, orient=VERTICAL)
scrbar.pack(side=RIGHT,fill=Y)

text = Text(root, width=80, height=10, state=NORMAL, background="white", foreground="black")
text.insert(INSERT, mytext)
text['state'] = DISABLED
text.pack()

text['yscrollcommand'] = scrbar.set
scrbar['command'] = text.yview

root.title("Matrix References")
root.mainloop()

这将产生以下结果(您可能需要调整文本格式):

在此输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接