tkinter滚动条没有滚动条

Thetukars

创建滚动条时,似乎其中没有滚动选项,即使其中的内容大于画布。画布位于框架内,因为我听说这是正确执行此操作的方法。我的代码的相关部分:

from tkinter import *

class UniversityProcessor:
    def __init__(self):
        self.root = Tk()
        self.rootWidth, self.rootHeight = 1200, 1200
        screenWidth, screenHeight = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
        xPosition, yPosition = (screenWidth/2) - (self.rootWidth/2), (screenHeight/2) - (self.rootHeight/2)
        self.root.geometry("%dx%d+%d+%d"%(self.rootWidth, self.rootHeight, xPosition, yPosition))
        self.root.title("University processor")
        
        self.updateUniText()
        self.root.mainloop()
    
    def updateUniText(self):
        self.textPixelTotal = 0
        
        self.frame = Frame(self.root, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
        self.frame.pack()
        
        self.canvas = Canvas(self.frame, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
        self.inner = Frame(self.canvas, bg = self.bg, width = self.rootWidth, height = self.rootHeight)
        self.canvas.create_window((0, 0), window = self.inner, anchor = "nw")

        for it in range(0, 50):
            label = Label(self.inner, bg = self.bg, text = f"Title {it + 1}", font = ("Arial", 20, "bold"))
            label.place(y = self.textPixelTotal, x = it)
            self.canvas.update()
            self.textPixelTotal += label.winfo_height()
        
        self.inner.configure(height = self.textPixelTotal)
        self.inner.place(x=0,y=0,anchor="nw")

        self.scroll = Scrollbar(self.frame, orient = VERTICAL, command = self.canvas.yview)
        self.scroll.pack(side = RIGHT, fill = Y)

        self.canvas.configure(yscrollcommand = self.scroll.set)
        self.canvas.bind("<Configure>", lambda e: self.canvas.configure(scrollregion = self.canvas.bbox("all")))
        self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
        
UniversityProcessor()

我不知道是否需要设置某种画布属性或滚动条属性,但它会填满整个屏幕...

接口

确实出现滚动条,它的大小正确,但是实际上没有滚动

acw1668

您不应该place()用来在画布内放置标签。您需要在画布内创建一个内部框架,并将所有标签放入该框架内:

    def updateUniText(self):
        textPixelTotal = 0
        
        self.frame = Frame(self.root, width=self.rootWidth, height=self.rootHeight)
        self.frame.pack()
        
        self.background = Canvas(self.frame, width=self.rootWidth, height=self.rootHeight)

        # an internal frame inside canvas
        self.internal = Frame(self.background)
        self.background.create_window(0, 0, window=self.internal, anchor="nw")
        
        # create labels inside the internal frame
        for i in range(0,200):
            label = Label(self.internal, bg=self.bg, text=f"Title {i+1}", font=("Arial", 20, "bold"))
            label.pack(anchor="w")
        self.background.update()
        
        self.scroll = Scrollbar(self.frame, orient=VERTICAL)
        self.scroll.pack(side=RIGHT, fill=Y)
        self.scroll.config(command=self.background.yview)
        self.background.config(width=self.rootWidth-20, height=self.rootHeight)
        self.background.config(yscrollcommand=self.scroll.set, scrollregion=self.background.bbox("all"))
        self.background.pack(side=LEFT, expand=True, fill=BOTH)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章