tkinter - 文本不在画布框架内展开

时间:2017-08-27 10:30:17

标签: text tkinter frame

enter image description here问题: 正如您在图像上看到的那样,text_fieldcan_frame(画布内的框架)并未填充紫色空白。

我的应用程序是如何构建的: 我的根窗口中有一个right_frame(紫色)和left_frame(粉红色)。在right_frame中,我放置了一个canvas我放置了一个框架,我称之为can_frame。 现在我can_frame放了两个text_fields

问题是,如何让can_frame以及text_fields(在can_frame中)填充紫色空间。

我制作的新简单应用程序的完整代码:

class Analyse_Window:
    def __init__(self, root):

        root.grid_rowconfigure   (0, weight=1)
        root.grid_columnconfigure(1, weight=1)


        self.left_frame = Frame( root, bg='pink',       height=580, width=450)
        self.right_frame = Frame(root, bg='light blue', height=580, width=450)
        self.left_frame.grid( row=0, column=0, sticky='nsew')
        self.right_frame.grid(row=0, column=1, sticky='nsew')

        self.right_frame.grid_columnconfigure(0, weight=1)
        self.right_frame.grid_rowconfigure(0, weight=1)

        # Create and setup canvas, scrollbar, and right frame with text fields.
        self.canvas = Canvas(self.right_frame, borderwidth=10, bg='purple')
        self.can_frame = Frame(self.canvas, bg='yellow')


        self.scrollbar  = Scrollbar(self.right_frame, orient='vertical',  command=self.canvas.yview)
        self.scrollbar2 = Scrollbar(self.right_frame, orient='horizontal',command=self.canvas.xview, width=25)
        self.canvas.configure(yscrollcommand=self.scrollbar.set, xscrollcommand=self.scrollbar2.set)



        # Grid canvas, frame, and scrollbars
        self.scrollbar.grid(row=0, column=1, sticky='ns')
        self.scrollbar2.grid(row=1, sticky='ew')
        self.canvas.grid(   row=0, column=0, sticky='nsew')
        self.can_frame.grid(row=0, column=0, sticky='nsew')

        self.canvas.grid_columnconfigure((0,1), weight=1)
        self.canvas.grid_rowconfigure((0,1), weight=1)


        self.canvas.create_window((4,4), window=self.can_frame, anchor='sw', tags="self.frame")
        self.can_frame.bind( "<Configure>",  self.onFrameConfigure)
        self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
        self.canvas.bind('<Configure>', self.foo)




        # Create text fields and put them in the canvas frame
        inp = Text(self.can_frame, width=40, relief='groove')
        inp.insert(END, 'this is a test')
        inp.configure(state=tkinter.DISABLED)
        inp.grid(row=0, column=0, sticky='nsew')

        inp2 = Text(self.can_frame, width=20, relief='groove')
        inp2.grid(row=2, column=0)


    def foo(self, event):
        w,h = event.width-100, event.height-100
        self.canvas.config(width=w, height=h)

    def onFrameConfigure(self, event):
        # update scrollregion after starting 'mainloop'
        # when all widgets are in canvas
        self.canvas.configure(scrollregion=self.canvas.bbox('all'))

    def _on_mousewheel(self, event):
        self.canvas.yview_scroll(int(-1*(event.delta/120)), 'units')




if __name__ == '__main__':
    root = Tk()
    b = Analyse_Window(root)
    root.mainloop()

您可以尝试将代码粘贴到自己的编辑器中并自行尝试吗?

1 个答案:

答案 0 :(得分:0)

画布未完全填充的部分原因是框架通常会填充其子画面(文本小部件和滚动条)请求的空间。通常,画布小部件是GUI布局的不良选择,除非您正在显示绘图元素(线条,椭圆和其他形状)。但在你的情况下,我所看到的只是其他tk小部件。除非您计划添加绘图元素,否则可以简化布局。所以下面的解决方案只是用它们的滚动条创建一个更简单的文本小部件布局,我理解可能不是你想要的。

import tkinter
from tkinter import *

class Analyse_Window:
    def __init__(self, root):

        root.grid_rowconfigure   (0, weight=1)
        root.grid_columnconfigure(1, weight=1)


        self.left_frame = Frame( root, bg='pink',       height=580, width=450)
        self.right_frame = Frame(root, bg='light blue', height=580, width=450)
        self.left_frame.grid( row=0, column=0, sticky='nsew')
        self.right_frame.grid(row=0, column=1, sticky='nsew')

        self.right_frame.grid_columnconfigure(0, weight=1)
        self.right_frame.grid_rowconfigure(0, weight=1)

        # Create and setup text widgets with scrollbars
        self.text_top = Text(self.right_frame, relief='groove', wrap='word', width=40)
        self.text_bot = Text(self.right_frame, relief='groove', wrap='word', width=20)
        self.text_top.insert(END, "This is the top text widget")
        self.text_bot.insert(END, "This is the bottom text widget")
        self.text_top['state'] = 'disabled'

        self.sty = Scrollbar(self.right_frame, orient='vertical',  command=self.text_top.yview)
        self.stx = Scrollbar(self.right_frame, orient='horizontal',command=self.text_top.xview, width=25)
        self.text_top.configure(yscrollcommand=self.sty.set, xscrollcommand=self.stx.set)

        self.sby = Scrollbar(self.right_frame, orient='vertical',  command=self.text_bot.yview)
        self.sbx = Scrollbar(self.right_frame, orient='horizontal',command=self.text_bot.xview, width=25)
        self.text_bot.configure(yscrollcommand=self.sby.set, xscrollcommand=self.sbx.set)

        # Grid the text widgets and scrollbars
        self.text_top.grid(row=0, column=0, sticky='news')
        self.sty.grid(row=0, column=1, sticky='ns')
        self.stx.grid(row=1, sticky='ew')
        self.text_bot.grid(row=2, column=0, sticky='nw')
        self.sby.grid(row=2, column=1, sticky='ns')
        self.sbx.grid(row=3, sticky='ew')

if __name__ == '__main__':
    root = Tk()
    b = Analyse_Window(root)
    root.mainloop()