当输入框大于tkinter中的窗口高度时,如何添加滚动条?

时间:2019-02-26 03:34:54

标签: python python-3.x tkinter jupyter

首先,当输入的矩形数少于24个时,它可以正常工作。当我将矩形的数量设置为大于24时,输入框超过了窗口高度,我看不到它。当输入框的数量超过窗口高度时,我想在右侧使用滚动条。该文档暗示只有“列表”,“文本框”,“画布”和“条目”小部件支持滚动条界面。那么,我该如何处理网格框?

注意:该代码是可验证的。

from tkinter import *

temp_recta = []

def save_content(top):
    for j in range(0, int(rectangles.get()) * 2):
        temp_recta.append(rectangle_values[j].get())

    root = Toplevel()

    l1 = Label(root, text="Size of Sheet[Height]:")
    l1.grid(row=0, column=0)

    l2 = Label(root, text="Size of Sheet[Width]:")
    l2.grid(row=1, column=0)

    height = StringVar()
    e1 = Entry(root, textvariable=height)
    e1.grid(row=0, column=1)

    width = StringVar()
    e2 = Entry(root, textvariable=width)
    e2.grid(row=1, column=1)

    b5 = Button(root, text="Proceed", width=12, command=lambda: save_sheetsize(e1, e2, root))
    b5.grid(row=2, column=0)
    b6 = Button(root, text="Back", width=12, command=lambda: (root.destroy(), top.deiconify()))
    b6.grid(row=2, column=1)

    root.title("Sheet Size")

def save_sheetsize(e1, e2, root):
    global x
    global y
    x = float(e2.get())
    y = float(e1.get())
    root.destroy()
    window.destroy()

rectangle_values = []
x = 0
y = 0
k = 0

def open_window():
    window.withdraw()
    global k
    top = Toplevel()
    top.title("Rectangles")

    for i in range(0, int(rectangles.get()) * 2):
        if (i % 2) == 0:
            l4 = Label(top, text="Size of rectangle:")
            l4.grid(row=i, column=0)

        rectangle_values.append(StringVar())
        en = Entry(top, textvariable=rectangle_values[i])
        en.grid(row=i, column=1)


    b3 = Button(top, text="Save", width=12, command=lambda: (top.withdraw(), save_content(top)))
    b3.grid(row=int(rectangles.get()) * 2 + 1, column=0)
    b4 = Button(top, text="Back", width=12, command=lambda: (top.destroy(), window.deiconify()))
    b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
    k = int(rectangles.get())

window = Tk()

l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)

rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)

b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

1 个答案:

答案 0 :(得分:-1)

这里有一个简单的类可以做到这一点。添加您的文本小部件,当窗口中充满文本时,它将为您提供滚动条。

import tkinter
class Scrollbar:
    def __init__(self,text):
        self.frame = text.master
        self.text = text
        self.text.configure(wrap='none')
        self.for_x_view()
        self.for_y_view()

    def for_x_view(self):
        # scroll Bar x For width
        scroll_x=tkinter.Scrollbar(self.frame, orient='horizontal',command=self.text.xview)
        scroll_x.config(command=self.text.xview)
        self.text.configure(xscrollcommand=scroll_x.set)
        scroll_x.pack(side='bottom', fill='x', anchor='w')
        return

    def for_y_view(self):
        # Scroll Bar y For Height
        scroll_y = tkinter.Scrollbar(self.frame)
        scroll_y.config(command=self.text.yview)
        self.text.configure(yscrollcommand=scroll_y.set)
        scroll_y.pack(side='right', fill='y')       
        return


if __name__ == '__main__':
    root = tkinter.Tk()
    pad = tkinter.Text(root,wrap='none')
    Scrollbar(pad)
    pad.pack()
    root.mainloop()