0

所以我想在窗口底部放置一个矩形。如何找到正确的 y 坐标?我的窗口是全屏打开的,所以 y 坐标并不总是相同的。

由于我对 python 使用的数学不太了解,而且有时我会为我的问题找到奇怪的解决方案,所以我尝试使用整数。当然,我用谷歌搜索了我的问题,但找不到有效的解决方案。

from tkinter import *
def run(): #I define it, so I can import it and use it in other files.
    w=Tk()
    w.state('zoomed')
    w.title('A title')
    bg=Canvas(w,bg='#808080',borderwidth=0,highlightthickness=0)
    bg.pack(fill='both',expand=True)

    ywindow=w.winfo_screenheight()
    yfooter=ywindow-30
    footer=Canvas(w,bg='#A5A5A5',borderwidth=2,highlightthickness=0)
    footer.place(height=30,width=w.winfo_screenwidth(),x=0,y=yfooter)

run()

我希望 tkinter 使用距离边界 30 像素的坐标作为 y,但它根本没有显示Canvas

4

2 回答 2

0

虽然我不确定我是否理解您的目标,但建议某人阅读tkinter文档,正如@Bryan Oakley 在评论中所做的那样,IMO 是一个残酷的笑话,该模块的文档太糟糕了......

无论如何,这里有一个猜测,显示了如何做我认为你可能想要的:

from tkinter import *

def run():
    win = Tk()
    win.state('zoomed')
    win.title('A title')

    FH = 30  # Footer height
    header_width = win.winfo_screenwidth()
    footer_width = win.winfo_screenwidth()

    footer_height = FH
    header_height = win.winfo_screenheight() - FH

    split = header_height / win.winfo_screenheight() # How much header occupies.

    header = Canvas(win, bg='#808080', borderwidth=0, highlightthickness=0,
                    width=header_width, height=header_height)
    header.place(rely=0, relheight=split, relwidth=1, anchor=N,
                 width=header_width, height=header_height)

    footer = Canvas(win, bg='#A5A5A5', borderwidth=2, highlightthickness=0,
                    width=footer_width, height=footer_height)

    footer.place(rely=split, relheight=1.0-split, relwidth=1, anchor=N,
                 width=footer_width, height=footer_height)

    if __name__ == '__main__':
        win.mainloop()

run()

运行的样子:

显示截图

于 2019-05-03T01:31:15.010 回答
0

这是因为页脚放置在根窗口的查看区域之外。如果您将根窗口设为全屏(使用w.wm_attributes('-fullscreen', 1)代替w.state('zoomed'),页脚将显示在根窗口的底部。但它仅适用于全屏模式。

您可以只使用footer.pack(fill=X)而不是footer.place(...),例如:

bg = Canvas(w, bg='#808080', bd=0, highlightthickness=0)
bg.pack(fill=BOTH, expand=True)

footer = Canvas(w, bg='#A5A5A5', bd=2, highlightthickness=0, height=30)
footer.pack(fill=X)

但是,如果将根窗口调整为小窗口,则此方法不起作用。

您可以使用grid()它来克服它:

w.rowconfigure(0, weight=1) # allow header expand vertically
w.columnconfigure(0, weight=1) # allow both header and footer expand horizontally

bg = Canvas(w, bg='#808080', bd=0, highlightthickness=0)
bg.grid(sticky='nsew')

footer = Canvas(w, bg='#A5A5A5', bd=2, highlightthickness=0, height=30)
footer.grid(sticky='ew')
于 2019-05-03T02:28:25.200 回答