【问题标题】:Python3 - AppJar (tkinter wrapper) change function of a buttonPython3 - AppJar(tkinter wrapper)按钮的更改功能
【发布时间】:2017-04-28 19:16:08
【问题描述】:

所以基本上我有一个按钮,我想更改功能。 Button 应该有 fnc HellWorld,如果我点击它 fnc 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/

【问题讨论】:

    标签: python-3.x tkinter wrapper


    【解决方案1】:

    您正在混合分配函数的 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()
    

    【讨论】:

    • 没有它们我得到一个 TypeError: HelloWorld() 接受 0 个位置参数,但给出了 1 个
    • 好的,我只为你安装了 appjar 并编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2013-10-06
    相关资源
    最近更新 更多