簡體   English   中英

連續刪除其中一個按鈕時如何“堆疊”按鈕? 蟒蛇,Tkinter

[英]How to "stack" buttons when deleting one of them in a row? Python, Tkinter

所以,我有生成的按鈕。 這些按鈕排成一行,看起來像這樣 當我刪除其中之一時,它看起來像這樣 我想要做的是“堆疊”它們,也許以某種方式刪除刪除按鈕后的那些,並將它們粘貼得更高? 所以它應該看起來像這個代碼:

todoLabel = Label(root, text=todo_new_Entry.get(), image=taskLabel, 
                  compound="center", borderwidth=0)         
todoLabel.place(x=62, y=todoTasks)
todoButton = Button(root, image=taskButton, borderwidth=0, command=disable)
todoButton.place(x=238, y=todoTasks-1)

每當單擊其他按鈕時都會生成它們,我想這無關緊要

也許嘗試將您的按鈕放在列表中。 嘗試以編程方式呈現它們,而不是使用絕對 (x, y) 位置。 然后當一個按鈕被刪除時,你可以將它從你的按鈕列表中刪除,然后簡單地重新渲染這些按鈕。

解決方案是使用除place其他東西。 由於您正在堆疊它們,因此pack是自然的選擇。 這是一個非常簡單的例子:

import tkinter as tk

class Item(tk.Frame):
    def __init__(self, master, text=None):
        super().__init__(master, borderwidth=0, relief="solid")
        label = tk.Label(self, borderwidth=1, relief="solid", text=text, width=20, height=2)
        buttonframe = tk.Frame(self, borderwidth=1, relief="solid", width=64, height=64)

        label.pack(side="left", fill="both", expand=True, padx=2)
        buttonframe.pack(side="right", fill="y", padx=2)

        delete_button = tk.Button(buttonframe, text="X", command=self.destroy)
        delete_button.pack(expand=True, padx=2, pady=2)

root = tk.Tk()
root.configure(bg="bisque")
for item_text in ("one", "two", "three", "four"):
    item = Item(root, text=item_text)
    item.pack(side="top", fill="x", padx=2, pady=2)

root.mainloop()

這是單擊帶有標簽“三”的項目旁邊的“x”后的屏幕截圖:

截屏

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM