小编典典

Tkinter-为每个LabelFrame添加滚动条

python

我试图让使用Tkinter的一个UI,我有问题要放三ScrollbarsLabelFrames

我有一个名为的数组self.que_lt_ver,其中包含一些名称:self.que_lt_ver = ['CARGA', 'MAQUINA', SOLTAR']。我想创建3个LabelFrames,每个都有一个Scrollbar。我Canvas为它创建了一个,但是它只显示最后一个窗口的Scroll:

在此处输入图片说明

那么,如何修改代码以显示每个滚动条LabelFrame

这是创建窗口的代码部分:

def createBox(self, window):
    for i in xrange(len(self.que_lt_ver)):
        mybox = LabelFrame(window, padx=5, pady=4)
        mybox.grid(row=i, column=0)
        self.createWindow(mybox, self.que_lt_ver[i], i)

def createWindow(self, box, lt_actual, i):
    self.canvas = Canvas(box, borderwidth=0)
    frame = Frame(self.canvas)
    vsb = Scrollbar(box, orient="vertical", command=self.canvas.yview)
    self.canvas.configure(yscrollcommand=vsb.set, width=1200, heigh=80)

    vsb.pack(side="right", fill="y")
    self.canvas.pack(side="left", fill="both", expand=True)
    self.canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")

    frame.bind("<Configure>", self.OnFrameConfigure)

    self.fillWindow(lt_actual, frame)

def fillWindow(self, lt_ver, frame):
    piezas = ['time: 39.41597 BT: 3025.5923', 'time: 21.637377 BT: 3025.5923', 'time: 52.185192 BT: 3025.5923', 'time: 57.804752 BT: 3025.5923', 'time: 47.700306 BT: 3025.5923', 'time: 21.1827 BT: 3025.5923', 'time: 35.244156 BT: 3025.5923', 'time: 47.26321 BT: 3025.5923']
    fechaentrada = ['26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '21-02-2014']
    fechasalida = ['26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '21-02-2014']
    horacomienzo = ['12:00', '12:39', '01:00', '01:52', '02:49', '03:36', '03:57', '12:00']
    horafinal = ['12:39', '01:00', '01:52', '02:49', '03:36', '03:57', '04:32', '12:47']
    ide = [0, 1, 2, 3, 4, 5, 6, 7]

    self.idpieza_w1 = Label(frame, text = "Id", width=20, font="bold")
    self.idpieza_w1.grid(row=0, column=0)
    self.pieza_w1 = Label(frame, text = "Pieza", width=20, font="bold")
    self.pieza_w1.grid(row=0, column=1)
    self.fechainiciopromo_w1 = Label(frame, text = "Dia inicio " + str(lt_ver), width=20, font="bold")
    self.fechainiciopromo_w1.grid(row=0, column=2)
    self.horainiciopromo_w1 = Label(frame, text = "Hora inicio "  + str(lt_ver), width=20, font="bold")
    self.horainiciopromo_w1.grid(row=0, column=3)
    self.fechafinalpromo_w1 = Label(frame, text = "Dia fin carga "  + str(lt_ver), width=20, font="bold")
    self.fechafinalpromo_w1.grid(row=0, column=4)
    self.horafinalpromo_w1 = Label(frame, text = "Hora final carga "  + str(lt_ver), width=20, font="bold")
    self.horafinalpromo_w1.grid(row=0, column=5)

    for i in xrange(len(piezas)):
        self.idtextos_w1 = Label(frame, text=str(ide[i]))
        self.idtextos_w1.grid(row=i+1, column=0)
        self.textos_w1 = Label(frame, text=str(piezas[i]))
        self.textos_w1.grid(row=i+1, column=1)
        self.fechainiciogrid_w1 = Label(frame, text=str(fechaentrada[i]))
        self.fechainiciogrid_w1.grid(row=i+1, column=2)
        self.horainiciogrid_w1 = Label(frame, text=str(horacomienzo[i]))
        self.horainiciogrid_w1.grid(row=i+1, column=3)
        self.fechafinalgrid_w1 = Label(frame, text=str(fechasalida[i]))
        self.fechafinalgrid_w1.grid(row=i+1, column=4)
        self.horafinalgrid_w1 = Label(frame, text=str(horafinal[i]))
        self.horafinalgrid_w1.grid(row=i+1, column=5)

def OnFrameConfigure(self, event):
    self.canvas.configure(scrollregion=self.canvas.bbox("all"))

提前致谢。


阅读 779

收藏
2021-01-20

共1个答案

小编典典

您创建了3个canvas,但是所有画布都设置在self.canvas中。

因此self.canvas对应于最后一个。您对其进行了3次配置,但从未两次配置。

查看我的解决方案(我删除了所有对象的东西):

from Tkinter import *

def createBox(window):
    list_ = ['CARGA', 'MAQUINA', 'SOLTAR']
    for i in xrange(3):
        mybox = LabelFrame(window, padx=5, pady=4)
        mybox.grid(row=i, column=0)
        createWindow(mybox, list_[i], i)

def createWindow(box, lt_actual, i):
    canvas = Canvas(box, borderwidth=0)
    frame = Frame(canvas)
    vsb = Scrollbar(box, orient="vertical", command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set, width=1200, heigh=80)

    vsb.pack(side="right", fill="y")
    canvas.pack(side="left", fill="both", expand=True)
    canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")

    # be sure that we call OnFrameConfigure on the right canvas
    frame.bind("<Configure>", lambda event, canvas=canvas: OnFrameConfigure(canvas))

    fillWindow(lt_actual, frame)

def fillWindow(lt_ver, frame):
    piezas = ['time: 39.41597 BT: 3025.5923', 'time: 21.637377 BT: 3025.5923', 'time: 52.185192 BT: 3025.5923', 'time: 57.804752 BT: 3025.5923', 'time: 47.700306 BT: 3025.5923', 'time: 21.1827 BT: 3025.5923', 'time: 35.244156 BT: 3025.5923', 'time: 47.26321 BT: 3025.5923']
    fechaentrada = ['26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '21-02-2014']
    fechasalida = ['26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '26-02-2014', '21-02-2014']
    horacomienzo = ['12:00', '12:39', '01:00', '01:52', '02:49', '03:36', '03:57', '12:00']
    horafinal = ['12:39', '01:00', '01:52', '02:49', '03:36', '03:57', '04:32', '12:47']
    ide = [0, 1, 2, 3, 4, 5, 6, 7]

    idpieza_w1 = Label(frame, text = "Id", width=20, font="bold")
    idpieza_w1.grid(row=0, column=0)
    pieza_w1 = Label(frame, text = "Pieza", width=20, font="bold")
    pieza_w1.grid(row=0, column=1)
    fechainiciopromo_w1 = Label(frame, text = "Dia inicio " + str(lt_ver), width=20, font="bold")
    fechainiciopromo_w1.grid(row=0, column=2)
    horainiciopromo_w1 = Label(frame, text = "Hora inicio "  + str(lt_ver), width=20, font="bold")
    horainiciopromo_w1.grid(row=0, column=3)
    fechafinalpromo_w1 = Label(frame, text = "Dia fin carga "  + str(lt_ver), width=20, font="bold")
    fechafinalpromo_w1.grid(row=0, column=4)
    horafinalpromo_w1 = Label(frame, text = "Hora final carga "  + str(lt_ver), width=20, font="bold")
    horafinalpromo_w1.grid(row=0, column=5)

    for i in xrange(len(piezas)):
        idtextos_w1 = Label(frame, text=str(ide[i]))
        idtextos_w1.grid(row=i+1, column=0)
        textos_w1 = Label(frame, text=str(piezas[i]))
        textos_w1.grid(row=i+1, column=1)
        fechainiciogrid_w1 = Label(frame, text=str(fechaentrada[i]))
        fechainiciogrid_w1.grid(row=i+1, column=2)
        horainiciogrid_w1 = Label(frame, text=str(horacomienzo[i]))
        horainiciogrid_w1.grid(row=i+1, column=3)
        fechafinalgrid_w1 = Label(frame, text=str(fechasalida[i]))
        fechafinalgrid_w1.grid(row=i+1, column=4)
        horafinalgrid_w1 = Label(frame, text=str(horafinal[i]))
        horafinalgrid_w1.grid(row=i+1, column=5)

def OnFrameConfigure(canvas):
    canvas.configure(scrollregion=canvas.bbox("all"))


tk = Tk()

createBox(tk)

tk.mainloop()
2021-01-20