如何使用可滚动的文本小部件创建GUI

时间:2018-04-07 13:37:27

标签: python tkinter

我想创建一个主窗口,底部有一个水平滚动条。

然后,此主窗口应包含不确定数量的带有垂直滚动条的较小文本小部件。

下面的代码将10个滚动的文本小部件打包成一个更大的文本小部件。

我的问题是“尝试一个”使用window_frame.pack(side="left", fill="both", expand=True)放置文本小部件。这给了我所需的高度(全屏)但我没有使用水平滚动条,所有十个小部件都无法查看。

在尝试二中,我使用text_box.window_create("1.0", window=window_frame)水平滚动条工作,我可以查看所有10个文本小部件,但我无法弄清楚如何使它们在垂直轴上填充屏幕。

有没有办法实现水平滚动功能并让文本小部件处于全屏高度(不测量字体大小)?

或者我是以完全错误的方式解决这个问题?

这是一个可行且简化的问题版本。只需按Escape即可摧毁窗口。

import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as St

root = tk.Tk()
root.bind("<Escape>", exit)
root.wm_attributes("-fullscreen", True)
#Mother Text Box to hold the windows
f = tk.Frame(root, background = "black")
f.pack(fill="both", expand=True)
vbar = ttk.Scrollbar(f, orient="horizontal")
text_box = tk.Text(f, xscrollcommand=vbar.set, wrap=tk.NONE, border=0, borderwidth=0)
text_box.pack(fill="both", expand=1)
vbar.pack(fill="x", side="bottom", pady=0)
vbar.config(command=text_box.xview)

window_frame = tk.Frame(text_box, background="yellow")

# This is attempt one #
#window_frame.pack(side="left", fill="both", expand=True)

# This is attempt two #
text_box.window_create("1.0", window=window_frame)

window_frame.configure(pady=2, padx=2)

text_box_list = []

for n in range(0, 10):
    text_box_list.append(St.ScrolledText(window_frame))
    text_box_list[n].pack(side="left", fill="both", expand=True)

root.mainloop()

def exit(root, event):
    root.destroy()

1 个答案:

答案 0 :(得分:1)

看一下这个例子:

import tkinter as tk
import tkinter.scrolledtext as St

class Example(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.pack(side="top", fill="both", expand=True)
        self.canvas = tk.Canvas(self, borderwidth=0)
        self.hbar = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hbar.set)

        self.canvas.pack(side="top", fill="both", expand=True)
        self.hbar.pack(side="bottom", fill="x")
        self.window_frame = tk.Frame(self.canvas)
        self.window_id = self.canvas.create_window((4,4), window=self.window_frame, anchor="ne")
        self.window_frame.bind("<Configure>", self.onFrameConfigure)
        self.canvas.bind("<Configure>", self.onCanvasConfigure)

        self.text_box_list = []
        for n in range(10):
            tb = St.ScrolledText(self.window_frame)
            tb.pack(side="left", fill='both', expand=True)
            self.text_box_list.append(tb)

        self.canvas.update_idletasks()      # refresh canvas

    def onFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def onCanvasConfigure(self, event):
        '''Set inner windowed frame to same height as canvas'''
        self.canvas.itemconfig(self.window_id, height=event.height)

if __name__ == "__main__":
    root=tk.Tk()
    root.geometry('1000x500+0+0')
    root.bind("<Escape>", exit)
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

而不是tk.Text.window_create我在这里使用tk.Canvas.create_window。但主要区别在于我更新了scrollregion并在调整大小时将内部窗口高度显式设置为画布高度。