如何使按钮逐渐出现在tkinter中

问题描述 投票:0回答:2

我用tkinter和Python 3制作了一个小应用程序,它在窗口顶部有四个按钮,用于形成菜单。它工作正常,但我想知道如何使按钮在窗口中出现一段时间从首次启动时从中心的单个按钮开始,而不是静态放置在中心。

到目前为止,这是我的脚本:

import tkinter as tk

class utilities(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.window()

    def window(self):

        self.pluginrun = tk.Button(self)
        self.pluginrun["text"] = "Run Existing Plugin"
        self.pluginrun["command"] = self.run_plugin
        self.pluginrun.pack(side="left")

        self.owning = tk.Button(self)
        self.owning["text"] = "Add A New Plugin"
        self.owning["command"] = self.plugin
        self.owning.pack(side="left")

        self.webpage = tk.Button(self)
        self.webpage["text"] = "Webpage"
        self.webpage["command"] = self.web
        self.webpage.pack(side="left")

        self.more_info = tk.Button(self)
        self.more_info["text"] = "More"
        self.more_info["command"] = self.more
        self.more_info.pack(side="left")

    def run_plugin(self):
        print('Running Plugin')

    def plugin(self):
        print('Available Extensions')

    def web(self):
        print("Opening Webpage To Python.org")

    def more(self):
        print('Made Entirely In Python')

root = tk.Tk()
root.geometry('500x500')
show = utilities(master=root)
show.mainloop()

这给出了这个结果:

enter image description here

首次打开时,我希望它看起来像这样:

enter image description here

并且在一段时间内,一次一个地出现更多按钮,直到它看起来像第一个图像。

如何才能做到这一点?

python python-3.x tkinter
2个回答
2
投票

您可以将所有按钮添加到列表中,然后使用重复定时方法以设定的间隔一次打包列表中的每个按钮。

我创建了一个计数器,我们可以使用它来跟踪列表中接下来要打包的按钮。

我还创建了一个新列表来存储所有按钮。

然后我修改了你的window()方法,将每个按钮添加到列表中。

最后一件事是创建一个定时方法,该方法将使用我创建的self.counter属性来跟踪下一个要打包的按钮。

在tkinter中,使用after()来保持定时循环或设置定时器的最佳方法是使用sleep()。在tkinter中使用wait()import tkinter as tk class utilities(tk.Frame): def __init__(self, master=None): super().__init__(master) self.pack() self.list_of_buttons = [] self.counter = 0 self.window() def window(self): for count in range(4): self.list_of_buttons.append(tk.Button(self)) pluginrun = self.list_of_buttons[0] pluginrun["text"] = "Run Existing Plugin" pluginrun["command"] = self.run_plugin owning = self.list_of_buttons[1] owning["text"] = "Add A New Plugin" owning["command"] = self.plugin webpage = self.list_of_buttons[2] webpage["text"] = "Webpage" webpage["command"] = self.web more_info = self.list_of_buttons[3] more_info["text"] = "More" more_info["command"] = self.more self.timed_buttons() def timed_buttons(self): if self.counter != len(self.list_of_buttons): self.list_of_buttons[self.counter].pack(side ="left") self.counter +=1 root.after(1500, self.timed_buttons) def run_plugin(self): print('Running Plugin') def plugin(self): print('Available Extensions') def web(self): print("Opening Webpage To Python.org") def more(self): print('Made Entirely In Python') root = tk.Tk() root.geometry('500x500') show = utilities(master=root) show.mainloop() 只会导致整个tkinter应用程序冻结。

看看下面的代码。

Buttons

-2
投票

Frame添加到你居中的Buttons中,然后当你添加更多的Frame时,root.update()应该居中。如果没有,你可能需要调用Frame,重新定位qazxswpoi。

© www.soinside.com 2019 - 2024. All rights reserved.