簡體   English   中英

tkinter 滾動條沒有滾動條

[英]tkinter scrollbar has no bar to scroll with

當我創建滾動條時,似乎沒有滾動選項,即使其中的內容比 canvas 大。 canvas 在框架內,因為我聽說這是正確執行此操作的方法。 我的代碼的相關部分:

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

我不知道是否需要設置某種 canvas 屬性或滾動條屬性,但它會填滿屏幕......

界面

滾動條確實出現了,它的大小是正確的,但是實際上沒有滾動

您不應使用place()將標簽放在 canvas 內。 您需要在 canvas 內部創建一個內部框架,並將所有標簽放在該框架內:

    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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM