简体   繁体   English

Python-Tkinter修复对象位置

[英]Python - Tkinter fix object position

Im currently testing around with python GUI and have made a script that takes 2 entered numbers from 2 textfields and upon a button press generates a block of labels (eg i enter 4 and 5 so it generates a 4x5 field of labels) 我目前正在使用python GUI进行测试,并制作了一个脚本,该脚本从2个文本字段中获取了2个输入的数字,并在按下按钮时生成了一个标签块(例如,我输入4和5从而生成了一个4x5的标签字段)

but now i want to do this: when i generate objects, i want to prevent them to - move - overlap my current objects (buttons, textfields). 但是现在我想这样做:当我生成对象时,我想防止它们-移动-重叠我当前的对象(按钮,文本字段)。 i can kind-of figure something for the overlapping, but every time i generate new stuff, everything moves around. 我有点想重叠的东西,但是每次我生成新东西时,一切都会四处移动。 Can i set a specific field in the grid to be "reserved" so that new stuff never goes in there? 我可以将网格中的特定字段设置为“保留”,以使新内容永远不会进入那里吗?

this is my current attempt - as you can see, its not overlapping anymore, but if the snowflakes are generated, the textboxes and buttons still "jump" apart for a small distance 这是我目前的尝试-如您所见,它不再重叠,但是如果生成了雪花,则文本框和按钮仍会“跳开”一小段距离

EDIT: the "jumps" are due to the font size of the added snowflakes - that still leaves my question on how i prevent this, as i dont want to be limited to small font sizes 编辑:“跳转”是由于所添加的雪花的字体大小-仍然对我如何防止这种情况留下了我的问题,因为我不想局限于小字体

from tkinter import *
wide = 0
deep = 0
entrytext = "test"
window = Tk()
window.title("test")
window.geometry('1000x1000')


ent = Entry(window)
ent.grid(column=0, row=1)

def GetClicked():
    global wide
    wide = ent.get()
    wide = int(wide)

btn2 = Button(window, text="Width", command=GetClicked)
btn2.grid(column=0, row=2)

ent2 = Entry(window)
ent2.grid(column=0, row=3)

def GetClicked2():
    global deep
    deep = ent2.get()
    deep = int(deep)

btn = Button(window, text="Depth", command=GetClicked2)
btn.grid(column=0, row=4)

def WingBut(column,row):
    lbl = Label(window, text="T", font=("Wingdings", 15))
    lbl.grid(column=column, row=row)
def clicked(wide,deep):
    h = 0
    j = 0
    while h in range (deep):
        i = 0
        h += 1
        while i in range(wide):
             if i > 2 or j > 5:
                 WingBut(i,j)
             i += 1
             if i == wide:
                 j += 1


btn = Button(window, text="Buttonspam",font=("Arial", 10),command=lambda: clicked(wide,deep))
btn.grid(column=0, row=0)






window.mainloop()

the textboxes and buttons still "jump" apart for a small distance 文本框和按钮仍然“跳开”一小段距离

This is due to the resulting size of the dynamically added labels (those labelled "T") being taller than the current row height for each row. 这是由于动态添加的标签(标记为“ T”的标签)的结果尺寸大于每行的当前行高度。 Because the row size must increase to accommodate the new label, the other widgets in the same row are also resized so that the overall height for the row is consistent. 因为必须增加行的大小才能容纳新标签,所以同一行中的其他窗口小部件也会调整大小,以使行的整体高度一致。 That resize is causing the jumping effect. 调整大小会导致跳跃效果。

One way to correct it would be to reduce the font size of the "T" labels. 一种解决方法是减小“ T”标签的字体大小。 Try setting it to 10 and the problem should go away. 尝试将其设置为10,问题应该消失。

Another way to solve it would be to set the minsize for each row to be the height of the tallest widget in the row, eg the "T" label widget height. 解决该问题的另一种方法是将每行的minsize设置为该行中最高的窗口小部件的高度,例如“ T”标签窗口小部件的高度。

for row in range(5):
    window.rowconfigure(row, minsize=36)

You can add the above code before you call window.mainloop() . 您可以在调用window.mainloop()之前添加以上代码。

I selected 36 because this makes the rows a minimum of 36 pixels high, and this is sufficient on my system to display the "T" without causing the row to resize. 我选择了36,因为这使行的最小高度至少为36个像素,这在我的系统上足以显示“ T”而不会导致行的大小调整。

If you don't want to hardcode the minsize you could calculate it dynamically. 如果您不想硬编码minsize ,则可以动态计算。

dummy = Label(window, text="T", font=("Wingdings", 20))
dummy.grid(row=0, column=0)
dummy.update_idletasks()        # seems to be required to get rendered size
height = dummy.winfo_height()
dummy.grid_forget()             # we don't want users seeing this widget

for row in range(5):
    window.rowconfigure(row, minsize=height)

That's one way to do it. 那是做到这一点的一种方法。 Possibly there is a better, more direct, way using the font itself, but you can research that if you're interested. 可能有一种更好,更直接的方式来使用字体本身,但是如果您有兴趣,可以进行研究。

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

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