1

I am trying to create a code that establishes a menu on my gui using python. The issue is, it keeps calling the function the null function when I create the New option under file. It is only supposed to go off when I click on new, but it goes off when I run the program. I used the print function in null to let me know when it's being called

class Board:
    def __init__(self, master):
        self.master = master
        self.setup()

    def null(self):
        print('o')


    def setup(self):


        board = tk.Canvas(self.master, width = 800, height = 800)
        board.pack()

        #Creates the Walls/Floor
        board.create_line(0, 790,800, 790, width = 20) #Creates Bottom Line
        board.create_line(10, 800,10, 100, width = 35) #Creates Left Wall
        board.create_line(790, 800, 790, 100, width = 35) #Creates Right Wall

        space = 20
        for x in range(6): #Creates pillars
            space += 108.5
            board.create_line(space, 800, space, 150, width = 20)

            board.pack() 

    def newgame(self):        
        self.menubar = tk.Menu(self.master)
        self.filemenu = tk.Menu(self.master, tearoff = 0)
        self.filemenu.add_command(label="New", command = self.null())
        self.menubar.add_cascade(label="File", menu=self.filemenu)
        self.filemenu.add_separator()
        self.master.config(menu=self.menubar)

1 Answer 1

1

self.null is being invoked (going off) because you are telling it to do so by placing () after it. Remember that Python uses (...) after a function name to invoke that function.

To fix the problem, simply remove the parenthesis:

self.filemenu.add_command(label="New", command=self.null)

Now, command is set to a reference of self.null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.