简体   繁体   English

tkinter.Toplevel大小自动适合其小部件

[英]tkinter.Toplevel size automatically fit its widgets

I have made my custom infobox class, InfoBox , that I am using in my application. 我已经制作了自定义的InfoBoxInfoBox ,该类正在我的应用程序中使用。 The tk.messagebox.showinfo did not suit my needs to the poor shape. tk.messagebox.showinfo不适合我的需要,以适应恶劣的环境。 But InfoBox does not adjust its size to fit the widgets I place inside. 但是InfoBox不会调整其大小以适合我放置在其中的小部件。 How can I make it as small as possible without cutting the widgets? 如何在不切割小部件的情况下使其尽可能小?

The class receives a string, msg , and a PhotoImage object, image , which are placed in the InfoBox. 该类接收一个字符串msg和一个PhotoImage对象image ,它们放置在InfoBox中。 I added a screenshot of one such InfoBox. 我添加了一个这样的InfoBox的屏幕截图。

class InfoBox(tk.Toplevel):
    def __init__(self, parent, msg, image):
        tk.Toplevel.__init__(self, parent)
        self.parent = parent
        self.msg = msg
        self.image = image

        self.title = "Gassy"
        self.font = font.Font(family="Optima", size=20)

        frame_left = tk.Frame(self)
        frame_right = tk.Frame(self)
        frame_left.grid(row=0, column=0, sticky=tk.NSEW)
        frame_right.grid(row=0, column=1, sticky=tk.NSEW)

        tk.Label(frame_left, image=self.image).grid(row=0, column=0, sticky=tk.N)

        textbox = tk.Text(frame_right, font=self.font)
        textbox.grid(row=0, column=0)
        textbox.insert(tk.END, self.msg)
        textbox.config(state=tk.DISABLED)

        tk.Button(frame_left, text="Den er grei!", font=self.font, command=self.destroy).grid(row=1, column=0)

在此处输入图片说明

As @kevin mentioned, it works as intended, the textwidget is mostly empty and occupies a large blank area, this is what makes you think that the geometry manager is not shrinking the window to the widgets. 正如@kevin所提到的,它可以按预期工作, textwidget大部分为空,并占据较大的空白区域,这就是让您认为几何图形管理器没有将窗口缩小到小部件的原因。

this: 这个:

(I removed the images and fonts that were not provided, and unnecessary) (我删除了没有提供的图像和字体,这是不必要的)

import tkinter as tk

class InfoBox(tk.Toplevel):
    def __init__(self, parent, msg):
        tk.Toplevel.__init__(self, parent)
        self.parent = parent
        self.msg = msg

        self.title = "Gassy"

        frame_left = tk.Frame(self)
        frame_right = tk.Frame(self)
        frame_left.grid(row=0, column=0, sticky=tk.NSEW)
        frame_right.grid(row=0, column=1, sticky=tk.NSEW)

#         textbox = tk.Text(frame_right) 
#         textbox.grid(row=0, column=0)
#         textbox.insert(tk.END, self.msg)
#         textbox.config(state=tk.DISABLED)

        tk.Button(frame_left, text="Den er grei!", command=self.destroy).grid(row=1, column=0)

root = tk.Tk()
info = InfoBox(root, '123 ' * 1000)
root.mainloop()

produces that: 产生:

在此处输入图片说明

whereas that: 而那:

import tkinter as tk

class InfoBox(tk.Toplevel):
    def __init__(self, parent, msg):
        tk.Toplevel.__init__(self, parent)
        self.parent = parent
        self.msg = msg

        self.title = "Gassy"

        frame_left = tk.Frame(self)
        frame_right = tk.Frame(self)
        frame_left.grid(row=0, column=0, sticky=tk.NSEW)
        frame_right.grid(row=0, column=1, sticky=tk.NSEW)

        textbox = tk.Text(frame_right) 
        textbox.grid(row=0, column=0)
        textbox.insert(tk.END, self.msg)
        textbox.config(state=tk.DISABLED)

        tk.Button(frame_left, text="Den er grei!", command=self.destroy).grid(row=1, column=0)

root = tk.Tk()
info = InfoBox(root, '123 ' * 1000)
root.mainloop()

produces this: 产生这个:

在此处输入图片说明

Clearly, the Toplevel subclass adjusts its size to the widgets it contains 显然, Toplevel子类将其大小调整为它包含的小部件

The test widget is displayed at a certain size, regardless of its content. 无论其内容如何,​​测试小部件均以一定的大小显示。 The Toplevel resizes around the widgets, NOT around whatever is inserted in the text widget; Toplevel围绕小部件调整大小,而不是围绕文本小部件中插入的内容进行调整; like with a text processor rudimentary window, the text processor does not shrink or expand as text is typed or edited. 就像使用文本处理器基本窗口一样,在键入或编辑文本时,文本处理器不会缩小或扩展。 The same applies here. 此处同样适用。
The keyword args width and height allow to configure the size (as a number of characters, or lines) of a text widget 关键字args widthheight允许配置文本小部件的大小(以字符数或行数为单位)

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

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