Tkinter-为每个LabelFrame添加滚动条 [英] Tkinter - Add scrollbar for each LabelFrame

查看:656
本文介绍了Tkinter-为每个LabelFrame添加滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tkinter制作UI,但在将三个Scrollbars放入LabelFrames时遇到问题.

I'm trying to make a UI using Tkinter and I'm having problems to put three Scrollbars inside LabelFrames.

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

I've an array called self.que_lt_verthat contains some names: self.que_lt_ver = ['CARGA', 'MAQUINA', SOLTAR'] . I want to create 3 LabelFrames and each one with a Scrollbar. I've created a Canvas for it, but it only shows the Scroll for the last window:

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

So how can I modify the code to show a scroll for each LabelFrame?

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

This is the part of the code where the windows are created:

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

谢谢.

推荐答案

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

You create 3 canvas, but all are set in self.canvas.

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

So self.canvas corresponds to the last one. You configure it 3 times, but never the two firsts.

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

See my solution (I remove all the object stuff) :

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

这篇关于Tkinter-为每个LabelFrame添加滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆