|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- from tkinter import *
- root=Tk()
- root.geometry("400x200")
- a=Button(root,text="1")
- a.pack(anchor="nw")
- b=Button(root,text="2")
- b.pack(anchor="se")
- c=Button(root,text="3")
- c.pack(anchor="ne")
- d=Button(root,text="4")
- d.pack(anchor="sw")
- mainloop()
复制代码
程序的运行结果,应该在4个角落啊?怎么不是啊?
在Tkinter中, pack方法的 anchor参数用于指定控件在其所在容器中的对齐方式。然而,在您的示例代码中,由于使用了默认的 expand=True选项,导致控件会尽可能地填充可用的空间。因此, anchor参数对控件的实际位置没有影响。
如果您希望将按钮分别放置在窗口的四个角落,可以考虑使用 place方法来精确控制按钮的位置。下面是一个示例:
- from tkinter import *
- root = Tk()
- root.geometry("400x200")
- a = Button(root, text="1")
- a.place(x=0, y=0, anchor="nw")
- b = Button(root, text="2")
- b.place(x=400, y=200, anchor="se")
- c = Button(root, text="3")
- c.place(x=400, y=0, anchor="ne")
- d = Button(root, text="4")
- d.place(x=0, y=200, anchor="sw")
- root.mainloop()
复制代码
这样,按钮a、b、c和d将根据指定的 x和 y坐标,并且以指定的 anchor对齐方式在窗口中的四个角落精确放置。
希望这个解答对您有帮助!如果还有其他问题,请随时提问。
|
|