0

I am researching about Tkinker in python. And I have question about this.Why my eventButton always auto run when Program is started although I don't click the button?

class Login_frame(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent
        self.initUI()
    def initUI(self):     
        frame1= Frame(self)
        frame1.pack(fill=X)
        lable1 = Label(frame1, text = "Username:", width=10)
        lable1.pack(side = LEFT, padx =5, pady =5)

        entry = Entry(frame1)
        entry.pack(fill=X, padx=5, expand=True)

        frame2 = Frame(self)
        frame2.pack(fill=BOTH)

        lable2 = Label(frame2,text="Password:", width=10)
        lable2.pack(side = LEFT, anchor=N, padx=5, pady=5)
        entry = Entry(frame2,show="*")
        entry.pack(fill=X, padx=5, pady=5)


        frame3=Frame(self)
        frame3.pack(fill=BOTH)

        frame4=Frame(self)
        frame4.pack(fill=BOTH)

        login= Button(frame4, text="Login",command = self.eventButtonLogin())
        login.pack(fill=X,side = LEFT, padx=5, pady=5)
        signup= Button(frame4, text="Sign up")
        signup.pack(fill=X,side = LEFT, padx=5, pady=5)
    def eventButtonLogin(self):
        showinfo(title="alert",message="OK")

app=Login_frame(None)
app.title("Login Frame")
app.mainloop()
falsetru
  • 357,413
  • 63
  • 732
  • 636
thanh lnh
  • 19
  • 3

2 Answers2

0

You need to replace following line:

login = Button(frame4, text="Login",command=self.eventButtonLogin())
                                                                 ↑↑

with:

login = Button(frame4, text="Login",command=self.eventButtonLogin)

In other word, remove ().

Because the line is calling the method before creating the button instead passing the function itself to Button constructor.

falsetru
  • 357,413
  • 63
  • 732
  • 636
0
# imports
from tkinter import *
from tkinter import ttk

# top level window
root = Tk()

# button and related actions
button = ttk.Button(root, text = "Click here")
button.pack()
button.config(command = <METHOD_TO_INVOKE>)
Dut A.
  • 1,029
  • 11
  • 22
  • While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – CodeMouse92 Apr 14 '16 at 18:05