指定两个嵌入“条目”的相对宽度在“文本”内小部件

发布于 2024-12-12 01:32:23 字数 460 浏览 0 评论 0原文

我在 Linux 和 Windows 上指定 Entry 小部件大小时遇到​​问题。这些条目是在 Linux 上创建的:

在 Linux 上,它们在文本小部件中看起来很好。有 2 个 Entry 单元格,由以下代码行创建:

tk.Entry(master, width=16)

宽度指定为 16 个字符长。

然而,在 Windows 上,单元格只占用一半的空间,我必须指定宽度 22,因为 Windows 上的字体尺寸较小。

我的问题是:有没有办法在文本小部件中指定这两个单元格的相对宽度,以便每个单元格占用父小部件的 1/2?

I have a problem in specifying Entry widget size on Linux and Windows. These entries were created on Linux:

On Linux, they look fine within the Text widget. There are 2 Entry cells one by one, created with this line of code:

tk.Entry(master, width=16)

The width is specified as being 16 characters long.

However, on Windows the cells take up only a half of the space and I have to specify the width of 22, because font size is smaller on Windows.

My question is: is there a way to specify a relative width of these two cells in the Text widget, so each cell takes 1/2 of the parent widget?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-12-19 01:32:23

在文本小部件内?不,不直接支持相对宽度。在一个框架内?是的。如果您将它们放入文本小部件中(我想,这样您就可以滚动它们),您必须自己管理宽度。您可以将绑定添加到文本小部件的 事件。当文本小部件更改大小时会触发此事件,您可以在此时调整所有小部件的大小。

最简单的方法是使用网格将它们放入框架中,然后将框架放入画布中以便可以滚动它。

这是一个例子:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.canvas = tk.Canvas(self, width=200, highlightthickness=0)
        self.vsb = tk.Scrollbar(orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)

        self.container = tk.Frame(self.canvas, borderwidth=0, highlightthickness=0)
        self.container.grid_columnconfigure(0, weight=1)
        self.container.grid_columnconfigure(1, weight=1)
        for i in range(30):
            e1 = tk.Entry(self.container)
            e2 = tk.Entry(self.container)
            e1.grid(row=i, column=0,sticky="ew")
            e2.grid(row=i, column=1,sticky="ew")
            e1.insert(0, "find %s" % i)
            e2.insert(0, "replace %s" % i)

        self.canvas.create_window((0,0), anchor="nw", window=self.container, tags="container")
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
        self.canvas.bind("<Configure>", self.OnCanvasConfigure)

    def OnCanvasConfigure(self, event):
        self.canvas.itemconfigure("container", width=event.width)
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

Within a text widget? No, there is no direct support for relative widths. within a frame? yes. If you are putting them in a text widget (I presume, so you can scroll them) you have to manage the widths yourself. You can add a binding to the <Configure> event of the text widget. This fires when the text widget changes size, and you can resize all the widgets at that point.

The easiest thing is to put them in a frame using grid, then put the frame in a canvas so you can scroll it.

Here's an example:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.canvas = tk.Canvas(self, width=200, highlightthickness=0)
        self.vsb = tk.Scrollbar(orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)

        self.container = tk.Frame(self.canvas, borderwidth=0, highlightthickness=0)
        self.container.grid_columnconfigure(0, weight=1)
        self.container.grid_columnconfigure(1, weight=1)
        for i in range(30):
            e1 = tk.Entry(self.container)
            e2 = tk.Entry(self.container)
            e1.grid(row=i, column=0,sticky="ew")
            e2.grid(row=i, column=1,sticky="ew")
            e1.insert(0, "find %s" % i)
            e2.insert(0, "replace %s" % i)

        self.canvas.create_window((0,0), anchor="nw", window=self.container, tags="container")
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
        self.canvas.bind("<Configure>", self.OnCanvasConfigure)

    def OnCanvasConfigure(self, event):
        self.canvas.itemconfigure("container", width=event.width)
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文