【问题标题】:Tkinter button exe before click (no bind)单击前的 Tkinter 按钮 exe(无绑定)
【发布时间】:2014-04-14 19:43:24
【问题描述】:

呃,该死的我的程序。我有更多问题:按钮的命令 (onExit()) 在导入时执行,但在单击按钮后不执行。

from Tkinter import *
import decoding
import encoding
import zxc

version="v0.0"

class widgets():
    def NewLabel(self, text, column, row, container):
        self.label=Label(container, text=text)
        self.label.grid(column=column,row=row)

    def NewEntry(self, container, text, column, row, action, key='<Return>', sticky="EW"):
        self.entry=Entry(container, textvariable=StringVar())
        self.entry.grid(column=column, row=row, sticky=sticky)
        self.entry.bind(key, action)
        StringVar().set(text)

    def NewButton(self, text, action, column, row, container, sticky="N"):
        self.button=Button(container, text=text, command=action)
        self.button.grid(column=column,row=row,sticky=sticky)
class actions():
    def OnEncode(self):
        try:
            zxc.encode()
            quit()
        except KeyboardInterrupt:
            print "goodbye"
            quit()

    def OnDecode(self):
        try:
            decoding.decode(version)
        except KeyboardInterrupt:
            print "Goodbye"
            quit()

    def OnExit(self):
        print __name__
        if __name__=="zxc":
            quit()

按钮包括:

    widgets().NewButton(u'Exit',actions().OnExit(),0,4,self)

现在运行时,程序会打印出“定义”并弹出带有不可点击按钮的窗口

【问题讨论】:

    标签: python button tkinter function


    【解决方案1】:

    当你这样做时:

    widgets().NewButton(..., actions().OnExit(), ...)
    

    ... 那么您将显式调用 OnExit() 的任何 actions() 返回的方法。然后将结果传递给按​​钮。

    相反,您需要传递对函数的引用,而不是直接调用函数。简而言之,去掉尾括号:

    widgets().NewButton(..., actions().OnExit, ...)
    

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 2020-12-04
      • 1970-01-01
      相关资源
      最近更新 更多