请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

anchor和side的区别

tk.Button(self.window, text=‘点我3’, width=20).pack(side=tk.LEFT, anchor=tk.E)
tk.Button(self.window, text=‘点我3’, width=20).pack(side=tk.LEFT, anchor=tk.E)

到底是在左边还是在右边,如果想把button的文字“点我”靠右显示怎么办?

正在回答 回答被采纳积分+3

1回答

吉米 2天前

如果需要让按钮组整体显示在容器右侧,修改pack参数:

# 所有按钮会从右到左排列

tk.Button(...).pack(side=tk.RIGHT)  # 第一个按钮在右侧

tk.Button(...).pack(side=tk.RIGHT)  # 第二个按钮在第一个的左侧



完整实例:

import tkinter as tk


class App:

    def __init__(self, master):

        self.window = master

        

        # 按钮1:文本靠右 + 按钮在容器左侧

        tk.Button(self.window, text='左按钮', width=20, anchor=tk.E).pack(side=tk.LEFT)

        

        # 按钮2:文本靠右 + 按钮在容器右侧

        tk.Button(self.window, text='右按钮', width=20, justify=tk.RIGHT).pack(side=tk.RIGHT)


root = tk.Tk()

app = App(root)

root.mainloop()


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号