可滚动的小部件

问题描述 投票:0回答:1

我试着用 https:/stackoverflow.coma6158872613512812。 有一个可滚动的按钮,但由于某些原因,滚动不工作。如果还不清楚我的目标是什么,当某个按钮被按下时,按钮被添加到可滚动的框架中。

from tkinter import *
import tkinter as tk  

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage,Task):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        frame.winfo_toplevel().geometry("1024x720")
        frame.configure(bg='#333130')
mylist = ['item1','item2','item3','item4','item5','item6','item7','item8','item9']

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        framecon = Frame(self)
        #framecon.configure(bg='red')

        global canvascoon
        canvascon = Canvas(framecon, height=100)
        global frame2
        frame2 = Frame(canvascon)
        myscrollbar=Scrollbar(framecon,orient="vertical",command=canvascon.yview) # will be visible if the frame2 is to to big for the canvas
        canvascon.create_window((0,0),window=frame2,anchor='nw')

        Button(self,text='go',command=lambda: meth()).pack()
        frame2.update() # update frame2 height so it's no longer 0 ( height is 0 when it has just been created )
        canvascon.configure(yscrollcommand=myscrollbar.set, scrollregion="0 0 0 %s" % frame2.winfo_height())
        canvascon.pack(side=LEFT)
        myscrollbar.pack(side=RIGHT, fill = Y)

        framecon.pack()

def meth():
    for item in mylist:
        c = Canvas(frame2, height=50, width=100, bg="red")


        lab = tk.Label(frame2, text='Item')
        lab_window = c.create_window(50, 12.5, window=lab)

        c.pack()


class Task(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)



app = SeaofBTCapp()
app.mainloop()

先谢谢大家的帮助。

python oop tkinter scrollbar frame
1个回答
0
投票

在这里,它是现在的工作:)

from tkinter import *
import tkinter as tk  

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage,Task):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        frame.winfo_toplevel().geometry("1024x720")
        frame.configure(bg='#333130')
mylist = ['item1','item2','item3','item4','item5','item6','item7','item8','item9']

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        framecon = Frame(self)
        #framecon.configure(bg='red')

        global canvascon
        canvascon = Canvas(framecon, height=100)
        global frame2
        frame2 = Frame(canvascon)
        global myscrollbar
        myscrollbar=Scrollbar(framecon,orient="vertical",command=canvascon.yview) # will be visible if the frame2 is to to big for the canvas
        canvascon.create_window((0,0),window=frame2,anchor='nw')

        Button(self,text='go',command=lambda: meth()).pack()

        canvascon.pack(side=LEFT)
        myscrollbar.pack(side=RIGHT, fill = Y)

        framecon.pack()

def meth():
    for item in mylist:
        c = Canvas(frame2, height=50, width=100, bg="red")


        lab = tk.Label(frame2, text='Item')
        lab_window = c.create_window(50, 12.5, window=lab)

        c.pack()
        frame2.update() # update frame2 height so it's no longer 0 ( height is 0 when it has just been created )
        canvascon.configure(yscrollcommand=myscrollbar.set, scrollregion="0 0 0 %s" % frame2.winfo_height())

class Task(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)



app = SeaofBTCapp()
app.mainloop()

© www.soinside.com 2019 - 2024. All rights reserved.