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

Text tkinter-文本未在画布框架内展开,text,tkinter,frame,Text,Tkinter,Frame,问题: 如图所示,text_字段和can_框架(画布内的框架)没有填充紫色的空白 我的应用程序是如何构造的: 我的根窗口中有一个右框(紫色)和左框(粉红色)。在右侧的

问题: 如图所示,
text_字段
can_框架
(画布内的框架)没有填充紫色的空白

我的应用程序是如何构造的: 我的根窗口中有一个
右框(紫色)和
左框(粉红色)。在
右侧的
中,我放置了一个
画布
,我在其中放置了一个框架,我称之为
can\u框架
。 现在,在这个
can\u框架中,我放置了两个
text\u字段

问题是,如何使
can_框架
以及
文本字段
(在
can_框架
中)填充紫色空间

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

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()
类分析\u窗口:
定义初始化(自,根):
root.grid_rowconfigure(0,权重=1)
root.grid\u columnconfigure(1,权重=1)
self.left_frame=frame(根,bg='pink',高度=580,宽度=450)
self.right_frame=frame(根,bg='浅蓝色',高度=580,宽度=450)
self.left_frame.grid(行=0,列=0,sticky='nsew')
self.right\u frame.grid(行=0,列=1,sticky='nsew')
self.right\u frame.grid\u column配置(0,权重=1)
self.right\u frame.grid\u row配置(0,权重=1)
#创建和设置画布、滚动条和带有文本字段的右框架。
self.canvas=canvas(self.right_frame,borderwidth=10,bg='purple')
self.can\u frame=frame(self.canvas,bg='yellow')
self.scrollbar=scrollbar(self.right\u frame,orient='vertical',command=self.canvas.yview)
self.scrollbar2=滚动条(self.right_frame,orient='horizontal',command=self.canvas.xview,width=25)
self.canvas.configure(yscrollcommand=self.scrollbar.set,xscrollcommand=self.scrollbar2.set)
#网格画布、框架和滚动条
self.scrollbar.grid(行=0,列=1,sticky='ns')
self.scrollbar2.grid(row=1,sticky='ew')
self.canvas.grid(行=0,列=0,sticky='nsew')
self.can\u frame.grid(行=0,列=0,sticky='nsew')
self.canvas.grid\u 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\u frame.bind(“,self.onFrameConfigure)
self.canvas.bind_all(“,self._在鼠标滚轮上)
self.canvas.bind(“”,self.foo)
#创建文本字段并将其放在画布框架中
inp=文本(self.can\u框架,宽度=40,浮雕='groove')
inp.insert(结束“这是一个测试”)
输入配置(状态=tkinter.DISABLED)
inp.grid(行=0,列=0,sticky='nsew')
inp2=文本(self.can_框架,宽度=20,浮雕='groove')
inp2.grid(行=2,列=0)
def foo(自身、事件):
w、 h=事件宽度-100,事件高度-100
self.canvas.config(宽度=w,高度=h)
def onFrameConfigure(自我,事件):
#启动“mainloop”后更新scrollregion
#当所有小部件都在画布中时
configure(scrollregion=self.canvas.bbox('all'))
鼠标滚轮上的def(自身、事件):
self.canvas.yview_滚动条(int(-1*(event.delta/120)),'units')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
b=分析窗口(根)
root.mainloop()

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

画布未完全填充的部分原因是框架通常会填充其子对象(文本小部件和滚动条)请求的空间。通常,画布小部件对于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()

画布没有完全填充的部分原因是框架通常会填充其子对象(文本小部件和滚动条)请求的空间。通常,画布小部件对于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()

请创建一个@BryanOakley我希望你现在能够帮助我,我刚刚更新了所有内容,我甚至将整个脚本复制到了一个新文档中,并删除了所有不必要的内容。请创建一个@BryanOakley我希望你现在能够帮助我,我刚刚更新了所有内容,我甚至将整个脚本复制到了一个新文档中,并删除了所有不必要的东西。我想一次滚动浏览我的文本小部件,然后只滚动一个水平滚动条。据我所知,这只能通过创建一个画布来实现,您可以将滚动条附加到画布上。如图所示:如果您真的需要,您可以创建一个(请参阅最后一个解决方案--#2)。我希望一次滚动所有文本小部件,然后只滚动一个水平滚动条。据我所知,这只能通过创建一个画布来实现,您可以将滚动条附加到画布上。如图所示:如果确实需要,可以创建一个(请参阅最后一个解决方案--#2)。