2013-11-23 46 views
1

我想創建一個代碼,在我的gui上使用python建立一個菜單。問題是,當我在文件下創建新選項時,它會一直調用函數null函數。當我點擊新的時候,它只會被關閉,但是當我運行程序時它會關閉。我使用的打印功能的空,讓我知道什麼時候它被稱爲Python GUI菜單問題

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

self.null被調用(打算關閉),因爲你告訴它通過後,將()這樣做。請記住,Python使用函數名稱後面的(...)來調用該函數。

要解決該問題,只要將括號:

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

現在,command被設定爲參考self.null