包装tkinter窗口扩展过去的框架

时间:2017-07-19 22:45:07

标签: python tkinter

我有一个我尝试布局的GUI,因此添加了屏幕文字以在每个方向上填充屏幕,但只能达到root.geometry的大小(' 300x600' )

相反,它正在远远超过框架的宽度。有没有办法确定这个尺寸,所以包装正确?到目前为止,我已经尝试了很多不能工作的东西。

编辑:要查看整个文本和滚动条,我必须设置root.geometry(' 684x418)

from tkinter import *

class Rhapsody_Gui:
    def __init__(self,root):
        Window = Frame(root, width=200, height=200)

        self.InWidget = Text(Window,bg='white',bd=3,fg='black',exportselection=0,height=1,wrap=WORD,insertofftime=0,insertbackground="white")

        Window.pack()
        self.InWidget.focus_set()



        self.scroll = Scrollbar(Window, orient=VERTICAL)
        self.OutWidget = Text(Window, yscrollcommand=self.scroll.set)#, height=30)
        self.OutWidget.bind("<Button 1>",self.Select)
        self.InWidget.bind("<Return>", self.Post)
        myList = ['words', 'that', 'should', 'be', 'printed']
        for x in myList:
            self.OutWidget['state'] = ['normal']
            self.OutWidget.insert(END, x +'\n')

        self.scroll.config(command=self.OutWidget.yview)
        self.InWidget.pack(side=TOP, fill='none')
        self.OutWidget.pack(side=LEFT, fill='none', expand=False)
        self.scroll.config(command=self.OutWidget.yview)
        self.scroll.pack(side=RIGHT, fill=Y, expand=False)#, ipadx=5)
        self.OutWidget['state'] = ['disabled']

    def Post(self,event):
        text = self.InWidget.get(1.0,2.0)
        self.InWidget.delete(1.0,2.0)
        self.OutWidget['state'] = ['normal']
        self.OutWidget.insert('end',text)
        self.OutWidget['state'] = ['disabled']
        return ("break")

    def Select(self,event):
        line_start = self.OutWidget.index("@%s,%s linestart" % (event.x, event.y))
        line_end = self.OutWidget.index("%s lineend" % line_start)
        self.OutWidget.tag_configure("highlight", background="yellow")
        self.OutWidget.tag_remove("highlight", 1.0, "end")
        self.OutWidget.tag_add("highlight", line_start, line_end)
        self.InWidget.focus_set()
        return ("break")

if __name__ == '__main__':
    root = Tk()
    root.geometry('300x600+600+100')
    App = Rhapsody_Gui(root)
    root.mainloop()

这就是我想要的样子,除了一直填到底部 This is what I want it to look like, except extended all the way to the bottom

2 个答案:

答案 0 :(得分:1)

您明确将OUtWidget设置为fill='none', expand=False。但是根据你的描述你想要:

self.OutWidget.pack(side=LEFT, fill='both', expand=True)

你需要从一个很小的文本窗口开始,因为它只会展开而不是收缩:

self.OutWidget = Text(Window, height=1, width=1, yscrollcommand=self.scroll.set)

并确保对包含框架执行相同的操作:

Window.pack(fill='both', expand=True)

指定Window的大小毫无意义,因为只要将任何内容打包到窗口中,这些设置就会被覆盖。

答案 1 :(得分:-2)

使用<window/parent>.resizable(width=False, height=True)