Python3 - AppJar(tkinter包装器)更改按钮的功能

时间:2017-04-28 19:16:09

标签: python-3.x tkinter wrapper

所以基本上我有一个按钮,我想改变功能。按钮应该有fnc HellWorld,如果我点击它再见GoodbyeWorld。

我的尝试:

from appJar import gui

app = gui()
app.setGeometry("300x300")


def HelloWorld(none):
    print("Hello World!")
    app.getButtonWidget("Button").config(command = GoodbyeWorld(none))

def GoodbyeWorld(none):
    print("Goodbye World!")
    app.getButtonWidget("Button").config(command = HelloWorld(none))

app.addButton("Button", HelloWorld, 1, 1)


app.go()

但如果我这样做,我的输出是:

Hello World!
Goodbye World!
Hello World!
Goodbye World!
Hello World!
Goodbye World!
Hello World!
Goodbye World!
Hello World!
Goodbye World!
....

然后我收到一些错误消息,并以RecursionError结束。

我做错了吗? (可能是..) 链接到AppJar:http://appjar.info/

1 个答案:

答案 0 :(得分:0)

您正在混合分配函数的appJar方法和tkinter方法。你需要坚持一个,因为我知道tkinter,我会建议使用tkinter方法:

from appJar import gui

app = gui()
app.setGeometry("300x300")


def HelloWorld():
    print("Hello World!")
    app.getButtonWidget("Button").config(command = GoodbyeWorld)

def GoodbyeWorld():
    print("Goodbye World!")
    app.getButtonWidget("Button").config(command = HelloWorld)

app.addButton("Button", None, 1, 1)
app.getButtonWidget("Button").config(command = HelloWorld) # set initial command

app.go()

您可能想要考虑为什么要重新分配该功能;这似乎很不寻常。对于您的示例,我只使用循环数据的单个函数:

from appJar import gui
from itertools import cycle

to_print = cycle(["Hello World!", "Goodbye World!"])
app = gui()
app.setGeometry("300x300")

def output(btn):
    print(next(to_print))

app.addButton("Button", output, 1, 1)
app.go()