【问题标题】:Deleting a Tkinter button删除 Tkinter 按钮
【发布时间】:2020-07-06 19:44:33
【问题描述】:

每当我使用 destroy() 函数删除 tkinter 中的按钮时,该按钮下方的 tkinter gui 窗口中的所有其他小部件都会向上移动,从而在我的屏幕底部创建一个间隙。我不希望这种情况发生。删除按钮后有什么方法可以防止小部件移动?另请注意,我必须将按钮放在放置在框架中的标签中,并且该框架位于主 gui 窗口中。这些是约束。

enter code here

from tkinter import *
screen = Tk()
screen.title("Kaun Banega Crorepati - The Game")
screen.geometry('1920x1080+0+0')
gameframe = Frame(screen, bg='#2e004d',width=1920,height=1080)
gameframe.pack()
panel = Label(gameframe, bg='#2e004d', width=1920, height=1080)
panel.pack()

def cmd():
    optionc.destroy()


optiona = Button(panel, text='A.option', font='Arial 18 bold', bg='black', 
fg='yellow')
optiona.pack(side='top', padx=40,pady=20)

optionb = Button(panel, text='B.option', font='Arial 18 bold', bg='black', 
fg='yellow')
optionb.pack(side='top', padx=40,pady=20)

optionc = Button(panel, text='C.option', font='Arial 18 bold', bg='black', 
fg='yellow')
optionc.pack(side='top', padx=40,pady=20)

optiond = Button(panel, text='D.option', font='Arial 18 bold', bg='black', 
fg='yellow')
optiond.pack(side='top', padx=40,pady=20)

destroybutton=Button(panel,text='destroy',font='Arial 18 bold', bg='black', 
fg='yellow',border=5,command=cmd)
destroybutton.pack(side='top', padx=40,pady=20)


screen.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter tkinter-layout


    【解决方案1】:

    问题:防止小部件在删除按钮后移动

    不要使用动态的Pack Geometry Manager,而是使用Place Geometry Manager

    核心点

    .place(x=..., y=...)
    


    参考


    注意:我推荐使用tk.Frame作为容器,也可以改为tk.Label

    定义FixedPanel(tk.Frame) 容器类。
    扩展:

    • button=tuple(text, command) 的序列
    • def button(text):返回对匹配textButton 的引用。
    import tkinter as tk
    
    
    class FixedPanel(tk.Frame):
        def __init__(self, parent, **kwargs):
            items = kwargs.pop('button', None)
            super().__init__(parent, **kwargs)
    
            for n, (text, command) in enumerate(items):
                button = tk.Button(self, text=text, command=command,
                                   font='Arial 18 bold', bg='black', fg='yellow')
                button.place(x=20, y=(n * 60) + 20)
    
        def button(self, text):
            for child in list(self.children):
                button = self.nametowidget(child)
                if button['text'] == text:
                    return button
    

    用法

    class FixedPanel 用作任何其他tkinter 小部件。
    对于.destroy()Button,使用panel.button(<text>) 获取Button 的引用

    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title("Kaun Banega Crorepati - The Game")
            self.geometry('800x500+0+0')
    
            self.panel = FixedPanel(self, bg='red', width=170, height=350,
                                    button=(('A.option', None),
                                            ('B.option', None),
                                            ('C.option', None),
                                            ('D.option', None),
                                            ('destroy', self.cmd))
                                    )
            self.panel.pack()
    
        def cmd(self):
            self.panel.button('C.option').destroy()
    
    
    if __name__ == "__main__":
        App().mainloop()
    

    用 Python 测试:3.5 - 'TclVersion':8.6 'TkVersion':8.6

    【讨论】:

    • 我理解您的观点,但包装和位置几何管理器不能一起工作。我已经为这个项目编写了大约 1000 行代码并使用了包管理器。那么有没有办法使用包几何管理器来做到这一点。
    • “打包和放置几何管理器不能一起工作”:如果在相同上使用,则属于此限制父容器小部件。 class FixedPanel 内部的所有内容都独立于外部。对象panel 使用self.panel.pack() 进行布局。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多