Tkinter 按钮帮助 [英] Tkinter buttons help

查看:27
本文介绍了Tkinter 按钮帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带按钮的类,它在构建 gui 时自动运行命令(我不希望它这样做),但之后不再工作.我究竟做错了什么?诸如 endcommand 之类的内置命令可以正常工作.

I have a class with a button, it runs the command automatically when the gui is constructed (which i dont want it to do) but then doesnt work again after. What am I doing wrong? Builtin commands such as endcommand work as they should.

相关摘录(忽略开头的缩进问题)

relevant excerpts (ignore the indent problem at the very beginning)

class GuiPart(object):    
def __init__(self, master, queue, endCommand):
    self.queue = queue
    # Set up the GUI
    #tkinter.Button(master, text='Done', command=endCommand).grid(row=6,column=6)

    tkinter.Button(text='Update Variables', command=self.updateValues()).grid(row=3)

    Lp_pacingState = tkinter.Label(text="p_pacingState")
    Lp_pacingState.grid(row=1, column=3)
    Tp_pacingState = tkinter.Label(bg="white", relief="ridge",justify="center",width=9)
    Tp_pacingState.grid(row=1, column=4)
    ....

    self.textBoxes = {"p_pacingState" : Tp_pacingState, "p_pacingMode" : Tp_pacingMode, 
                 "p_hysteresis" : Tp_hysteresis, "p_hysteresisInterval" : Tp_hysteresisInterval,
                 "p_lowrateInterval" : Tp_lowrateInterval, "p_vPaceAmp" : Tp_vPaceAmp,
                 "p_vPaceWidth" : Tp_vPaceWidth, "p_VRP" : Tp_VRP}

#def updateValues(self,input):
def updateValues(self):
    testInput = ["p_pacingState=3", "garbage=poop", "p_VRP=5"]
    for updates in testInput:
        print("zzzz")
        var = updates.split("=")
        try:
            self.textBoxes[var[0]].config(text = var[1])
        except:
            pass

所以我在构建 gui 时打印了 3 次zzzz"(尽管标签不会更新它们的文本),此后按钮不起作用.另外,如果有更好的方法来更新盒子,请告诉我.我从流中获得输入,没有特定的顺序或相关性.

So I get "zzzz" printed 3 times at construction of gui (labels dont update their text though) and the button doesnt work after that. Also if theres a better way to update boxes please tell me. I get input from a stream in no particular order or relevance.

提前致谢

推荐答案

当你这样做时:

command=self.updateValues()

您正在调用函数 self.updateValues(因为 ()).该函数调用的结果被分配给不是您想要的命令属性.您需要删除 () 以便 command 属性获得对该方法的引用,而不是调用该方法的结果.

You are calling the function self.updateValues (because of the ()). The result of that function call is being assigned to the command attribute which is not what you want. You need to remove the () so that the command attribute gets a reference to the method rather than the result of calling the method.

这篇关于Tkinter 按钮帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆