【问题标题】:tkinter - text not expanding inside the frame of a canvastkinter - 文本没有在画布的框架内扩展
【发布时间】:2018-02-04 20:05:11
【问题描述】:

问题: 正如您在图像上看到的,text_fieldcan_frame(画布内的框架)没有填充紫色空白。

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

问题是,如何让can_frametext_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()

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

【问题讨论】:

  • @BryanOakley 我希望你现在能帮助我,我刚刚更新了所有内容,我什至将整个脚本复制到了一个新文档中,并删除了所有不必要的东西。

标签: text tkinter frame


【解决方案1】:

画布未完全填满的至少部分原因是框架通常会填满其子项(文本小部件和滚动条)请求的空间。通常,除非您要显示绘图元素(线条、椭圆和其他形状),否则画布小部件对于 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()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 2021-09-28
  • 2021-11-26
  • 2020-03-17
  • 1970-01-01
  • 2012-04-03
  • 2017-02-22
相关资源
最近更新 更多