为了账号安全,请及时绑定邮箱和手机立即绑定

如何删除 tkinter 中按钮之间的空间?

如何删除 tkinter 中按钮之间的空间?

GCT1015 2023-12-20 16:11:06
在我的应用程序中,我看到彩色按钮之间有很多我实际上不想要的空间,我该如何删除它们?我尝试使用button.pack(pady=0)按钮但没有任何效果我已在此处添加了我的代码,并尝试仅放置代码的相关部分我添加了画布,这样我就可以添加滚动条。我使用按钮是因为我想添加一个方法来显示可以编辑/删除任务的页面。class TodoFrame(Frame):    def __init__(self, parent, controller):        Frame.__init__(self, parent)        self.canvas = Canvas(self)        self.scrollbar = Scrollbar(self, orient="vertical", command=self.canvas.yview)        task_frame = Frame(self.canvas, height=self.winfo_height()-100)        task_frame.pack(fill="x", expand=True)        self.canvas.create_window(0, 0, anchor='center', window=task_frame, width=self.winfo_width(), height=self.winfo_height()-100)        priority_colors = ("#00CED1", "#00FA9A", "#FF6347", "#B0C4DE")  # colors for buttons        # the db.fetch_incomplete_tasks() fetches tasks from mysql database        # and returns a list of tuples containing title, description, priority and completion status of the task        self.incomplete_tasks = sorted(db.fetch_incomplete_tasks(), key=lambda x: x[2])  # sorting list by priority        if self.incomplete_tasks:            incomplete_task_label = Label(task_frame, text="Incomplete Tasks", font=('calibri', 16))            incomplete_task_label.pack(padx=(50, 0), anchor="center")                        for task in self.incomplete_tasks:                title, description, priority, status = task                task_btn = Button(task_frame, text=title, bg=priority_colors[priority-1], bd=0, width=25, wraplength=400, justify="left", pady=5)                task_btn.pack(expand=True, padx=(50,0))
查看完整描述

1 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

所以问题是我在这一行设置了画布的高度 self.canvas.create_window(0, 0, anchor='center', window=task_frame, width=self.winfo_width(), height=self.winfo_height()-100)

这导致了额外空间的形成,而按钮之间的空间就是为了填补这个间隙



查看完整回答
反对 回复 2023-12-20
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信