简体   繁体   English

Tkinter:tk.Text 小部件中的高度/宽度缩放问题

[英]Tkinter: Issue with height/width scaling in tk.Text widget

I cannot seem to get the scroll on this text to work.我似乎无法让此文本上的滚动条起作用。 The text will scroll to a certain extent, but then not appear afterwards.文本会滚动到一定程度,但之后不会出现。 I believe that the height of the text widget is not what I want.我相信文本小部件的高度不是我想要的。 For instance, the image below shows only about a half of what the actual result is for 遺伝子 (which I can find out by attempting to drag from a piece of text in the middle of the frame to the bottom).例如,下图只显示了遗伝子实际结果的一半左右(我可以通过尝试从框架中间的一段文本拖动到底部来找到)。 The width also is not the same size of the frame I would like it to be: the number 23 was just something that appeared to work.宽度也与我想要的框架大小不同:数字 23 只是看起来可行的东西。

If I do如果我做

txt = tk.Text(self.SEARCH_RESULTS_TEXT_FRAME,width=self.SEARCH_RESULTS_FRAME_WIDTH,height=20,background='#d9d9d9',relief=RIDGE)

... the width becomes much too large. ...宽度变得太大了。 I would have thought that it would only take up the size of the frame, which is 240.我原以为它只会占用框架的大小,即 240。

Note the SEARCH_RESULTS_TEXT_FRAME is the frame in red.注意SEARCH_RESULTS_TEXT_FRAME是红色的框架。

Relevant code:相关代码:

    #search text frame
    self.SEARCH_RESULTS_FRAME_HEIGHT = 240 #240
    self.SEARCH_RESULTS_FRAME_WIDTH = self.TITLE_FRAME_WIDTH - 23
    self.SEARCH_RESULTS_TEXT_FRAME = Frame(self.SEARCH_RESULTS_FRAME,height=self.SEARCH_RESULTS_FRAME_HEIGHT,width=self.SEARCH_RESULTS_FRAME_WIDTH)
    self.SEARCH_RESULTS_TEXT_FRAME.place(x=10,y=10,anchor = NW)
    self.SEARCH_RESULTS_TEXT_FRAME.config(background ="#adadad")

def print_dict_to_frame(self,results_list):
    txt = tk.Text(self.SEARCH_RESULTS_TEXT_FRAME,width=34,height=20,background='#d9d9d9',relief=RIDGE)
    txt.place(x=0,y=0)
    txt.tag_configure('header',justify = 'center',font=("Meiryo",12,'bold'))
    txt.tag_configure('entry',font=('Meiryo',8))
    for r_list in results_list:
        header = r_list[0]
        entry = r_list[1]
        txt.insert(tk.END, "{}\n".format(header),'header')
        for single_result in entry:
            txt.insert(tk.END,single_result+"\n",'entry')
    txt.configure(state=DISABLED)

框架的相关图像。请注意,它位于文本条目的后面(红色)

How can I get the text widget to only take up the width of the frame and the height of the frame, allowing for scrolling?如何让文本小部件仅占用框架的宽度和框架的高度,从而允许滚动?

If I do txt = tk.Text(self.SEARCH_RESULTS_TEXT_FRAME,width=self.SEARCH_RESULTS_FRAME_WIDTH,height=20,background='#d9d9d9',relief=RIDGE)... the width becomes much too large. I would have thought that it would only take up the size of the frame, which is 240.如果我这样做txt = tk.Text(self.SEARCH_RESULTS_TEXT_FRAME,width=self.SEARCH_RESULTS_FRAME_WIDTH,height=20,background='#d9d9d9',relief=RIDGE)... the width becomes much too large. I would have thought that it would only take up the size of the frame, which is 240. txt = tk.Text(self.SEARCH_RESULTS_TEXT_FRAME,width=self.SEARCH_RESULTS_FRAME_WIDTH,height=20,background='#d9d9d9',relief=RIDGE)... the width becomes much too large. I would have thought that it would only take up the size of the frame, which is 240.

The width option specifies a width in the number of characters, not pixels. width选项以字符数而不是像素数指定宽度。 If you use the value 240, the width will be 240 multiplied times the width of an average character in the font you are using.如果您使用值 240,宽度将是 240 乘以您正在使用的字体中的平均字符宽度。

How can I get the text widget to only take up the width of the frame and the height of the frame, allowing for scrolling?如何让文本小部件仅占用框架的宽度和框架的高度,从而允许滚动?

Give the text widget a width and height of 1, and then let the geometry manager ( pack , place , or grid ) be responsible for stretching it to fit the frame.为文本小部件设置宽度和高度 1,然后让几何管理器( packplacegrid )负责拉伸它以适应框架。

Since you're using place , you can use the relwidth and relheight options to make it the full size of the frame:由于您使用的是place ,因此您可以使用relwidthrelheight选项使其成为框架的完整尺寸:

txt.place(x=0,y=0, anchor="nw", relwidth=1.0, relheight=1.0)

Personally, I recommend using pack or grid in almost all cases.就个人而言,我建议几乎在所有情况下都使用packgrid It's much easier to create a responsive UI with them than it is with place .使用它们创建响应式 UI 比使用place更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM