Tkinter - 如何将滚动条添加到弹出窗口

时间:2014-01-23 11:53:30

标签: python scroll tkinter scrollbar

我有一个小程序弹出窗口并显示一些保存在数组中的文本:

enter image description here

我想要做的是在项目的右侧添加一个滚动,这样如果项目数组很长,我就没有问题显示数组中包含的所有值。

这就是我想要的。静态标题(Id,Pieza等),以及滚动条内的内容本身。抱歉可怜的Photoshop: enter image description here

这是我的代码:

from Tkinter import *
import Image
import ImageTk
import tkFileDialog
import ttk


class Ventanas(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.master = master
        self.frameOne = Frame(self.master)
        self.frameOne.grid(row=0,column=0)
        self.initial_window()

def initial_window(self):
    self.button1 = Button(self.frameOne, text="go popup window", width=40, command=self.leerjson)
    self.button1.grid(row=1, column=0, padx=(8,8), pady=(10,5))

def leerjson(self):
    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.imprimir_carga(piezas, ide, fechaentrada,fechasalida, horacomienzo, horafinal)

def imprimir_carga(self, piezas, ide, fechaentrada,fechasalida, horacomienzo, horafinal):

    window2 = Toplevel(self)

    # TOP WINDOW (w1) - CARGA
    self.idpieza_w1 = Label(window2, text = "Id", width=20, font="bold")
    self.idpieza_w1.grid(row=0, column=0)
    self.pieza_w1 = Label(window2, text = "Pieza", width=20, font="bold")
    self.pieza_w1.grid(row=0, column=1)
    self.fechainiciopromo_w1 = Label(window2, text = "Dia inicio carga", width=20, font="bold")
    self.fechainiciopromo_w1.grid(row=0, column=2)
    self.horainiciopromo_w1 = Label(window2, text = "Hora inicio carga", width=20, font="bold")
    self.horainiciopromo_w1.grid(row=0, column=3)
    self.fechafinalpromo_w1 = Label(window2, text = "Dia fin carga", width=20, font="bold")
    self.fechafinalpromo_w1.grid(row=0, column=4)
    self.horafinalpromo_w1 = Label(window2, text = "Hora final carga", width=20, font="bold")
    self.horafinalpromo_w1.grid(row=0, column=5)

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

if __name__ == "__main__":
    root = Tk()
    aplicacion = Ventanas(root)
    root.mainloop()

我已阅读Scrollbar文档,ListBoxText似乎很容易,但如果我要添加{{1},我就完全迷失了一些Scrollbar

提前致谢

1 个答案:

答案 0 :(得分:1)

您不能直接滚动一个装满小部件的框架。你需要做的是在画布中嵌入一个包含所有小部件的框架,然后将滚动条附加到画布上。之前已经提出并回答过:

Adding a scrollbar to a group of widgets in Tkinter