【问题标题】:How to place a canvas object at the bottom of the window?如何在窗口底部放置一个画布对象?
【发布时间】:2019-05-02 20:02:44
【问题描述】:

所以我想在窗口底部放置一个矩形。如何找到正确的 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

【问题讨论】:

  • 如果你想要它在底部,否则在同一个容器中使用.pack(),那么只需使用.pack(side=BOTTOM)
  • 你是问放置矩形,还是问放置cnavas?如果您询问有关画布的问题,请阅读place 上的文档。您可以指定相对于边缘的坐标。

标签: python tkinter tkinter-canvas tkinter-layout


【解决方案1】:

虽然我不确定我是否理解您的目标,但向某人建议他们阅读 tkinter documentation,就像 @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()

跑步的样子:

【讨论】:

    【解决方案2】:

    这是因为页脚放置在根窗口的查看区域之外。如果您将根窗口设为全屏(使用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')
    

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多