所以我想在窗口底部放置一个矩形。如何找到正确的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
。
答案 0 :(得分:0)
虽然我不确定我是否理解您的目标,但建议某人阅读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()
运行时看起来像什么
答案 1 :(得分: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')