本文介绍了有没有办法总是有一个相对于另一个小部件的地方小部件(如包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我使用 place 方法让小部件与其他小部件重叠,但它的位置是相对的(使用 winfo)到使用 pack 的小部件.当父框架调整大小时,包装位置会改变,但放置位置不会.这是我的代码:

So, I'm using the place method to have a widget overlap other widgets, but its position is relative (with winfo) to a widget that uses pack. When the parent frame is resized, the pack position will change, but the place position will not.This is my code:

from tkinter import *
root = Tk()
root.geometry("200x300")

search = Entry(root)
search.pack()

search.update()
x = search.winfo_x()
y = search.winfo_y()
width = search.winfo_width()
height = search.winfo_height()

frame = LabelFrame(root, width=width, height=200)
frame.place(x=x, y=y+height)

root.mainloop()

调整窗口大小时,LabelFrame 保持在其 x 和 y 位置.Entry 小部件将用作搜索栏,我希望在其下自动完成.条目小部件下将有小部件,自动完成功能只会在您键入时出现(尽管这不是我要找的.如果您需要,它只是更多说明).那么,有没有办法让位置小部件始终相对于包小部件.如果您有任何答案,谢谢:)

The LabelFrame stays in its x and y position when the window is resized. The Entry widget will be used as a search bar and I want autocompletion under it. There will be widgets under the entry widget and the autocompletion will only appear when you are typing (That's not what I'm looking for though. Its just more exposition if you need it). So, is there a way to have the place widget always be relative to the pack widget. If you have any answers, thank you:)

推荐答案

如果您的目标是将一个小部件相对于另一个小部件放置,place 可让您做到这一点.它非常适合工具提示或其他不适合正常布局的瞬态小部件.

If your goal is to put one widget relative to another, place lets you do that. It's great for things like tooltips or other transient widgets that don't otherwise fit into a normal layout.

最简单的方法是让小部件成为控制小部件的子部件.例如,对于相对于搜索框放置的框架,您可以将其设为搜索框的子项.如果这样做不方便,您可以使用 in_ 参数告诉 place 哪个小部件是另一个小部件.

The easiest way to do that is to make the widget a child of the controlling widget. For example, for your frame to be placed relative to the search box you can make it a child of the search box. If it's inconvenient to do that, you can use the in_ parameter to tell place which widget is the other widget.

例如,要将标签框直接放置在搜索框下方并与搜索框宽度相同,您可以这样做:

For example, to place your labelframe immediately below the search box and with the same width as the search box you might do it something like this:

frame.place(
    in_=search,
    bordermode="outside",
    anchor="nw",
    relx=0,
    rely=1.0,
    y=5,
    relwidth=1.0
)

这就是选项的含义:

  • in_=search:相对于搜索框放置框架
  • bordermode=outside":相对测量值来自边界外(默认为inside")
  • anchor="nw":放置小部件,使框架的西北角位于计算的坐标处
  • relx=0:将锚点放置在距搜索框左边缘 0% 处
  • rely=1.0:将框架放置在搜索框高度的 100% 处
  • y=5:向计算位置添加 5 个像素,使其浮动在窗口下方一点
  • relwidth=1.0:使框架的宽度为搜索框宽度的 100%.
  • in_=search: place the frame relative to the search box
  • bordermode="outside": relative measurements are from the outside of the border (default is "inside")
  • anchor="nw": place the widget so that the northwest corner of the frame is at the computed coordinate
  • relx=0: place the anchor point 0% from the left edge of the search box
  • rely=1.0: place the frame at 100% of the height of the search box
  • y=5: add 5 pixels to the computed position so it floats just a little below the window
  • relwidth=1.0: make the width of the frame 100% the width of the search box.

显然你不必使用 y=5,我只是添加它来说明使用 relyy 的附加行为.

Obviously you don't have to use y=5, I just added it to illustrate the additive behavior of using rely and y.

这篇关于有没有办法总是有一个相对于另一个小部件的地方小部件(如包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:39