每次程序启动时应用随机函数(来自其中一些)

时间:2017-12-21 10:03:54

标签: python function

我创建了几个函数来改变我的应用程序的主题。下面给出了一些例子:

def redtheme():
    text.config(background="light salmon", foreground="red",
            insertbackground="red")

def greentheme():
    text.config(background="pale green", foreground="dark green",
                insertbackground="dark green")

def bluetheme():
    text.config(background="light blue", foreground="dark blue",
                insertbackground="blue")

(text是文本小部件的名称)

我想创建一个函数,使应用程序启动时随机运行其中一个函数。 换句话说,我想要一个在应用程序启动时执行的函数,该函数将从random.choice()中选择一个函数并执行该函数:

full = (redtheme, greentheme, bluetheme)
selected = random.choice(full)
# here, it could be text.config(full)?? or what?

如何在应用程序启动时执行三个函数之一

1 个答案:

答案 0 :(得分:2)

AS也在CommonSense的评论中提出以下代码是您想要的一个小例子:

trait ValRequireTrait<T: ValTrait = Self> {}

上面的代码基本上利用了这样一个事实:你可以为变量分配函数引用,这里首先将函数名引用中的随机选择分配给trait ValRequireTrait<T: ValTrait = Self>: ValTrait<T> {} ,然后将import tkinter as tk import random root = tk.Tk() text = tk.Text(root) def redtheme(): text.config(background="light salmon", foreground="red", insertbackground="red") def greentheme(): text.config(background="pale green", foreground="dark green", insertbackground="dark green") def bluetheme(): text.config(background="light blue", foreground="dark blue", insertbackground="blue") full = (redtheme, greentheme, bluetheme) selected = random.choice(full) selected() text.pack() root.mainloop() 作为函数调用。

另外,请参阅以下示例,立即调用随机选择的函数:

selected