简体   繁体   中英

Tkinter button exe before click (no bind)

Ehh, damn my program. I have more problems with it: button's command (onExit()) executes on import, but not after clicking button.

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()

button including:

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

on running right now, program prints me 'definitions' and window with unclickable button pops up

When you do this:

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

... then you are explicitly calling the OnExit() method of whatever actions() returns. The result of this is then passed on to the button.

Instead, you need to pass a reference to the function, rather than calling the function directly. In short, remove the trailing parenthesis:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM