破碎的下拉菜单代码

时间:2016-10-14 05:36:01

标签: windows tkinter python-3.5

我正在关注https://www.youtube.com/watch?v=PSm-tq5M-Dc教程,在gui中执行下拉菜单。在视频中,代码可以工作,但我也不能使用它,我认为它可能与不同的python版本有关。

C:\Users\TheSheep\Desktop\pygui>python dropdown.py
  File "dropdown.py", line 14
    subMenu.add_command(label="New Project..."), comand=doNothing
    ^
SyntaxError: can't assign to function call

这是错误

var Calculator = (function() {
  function Calculator(fields, viewport) {
    var self = this;
    this.fields = fields;
    this._state = { result: 0 };
    this.decimals = 2;
    this.viewport = viewport;
    
    this.fields
      .forEach(function(field) {
        field.addEventListener('change', self.onChange.bind(self, field))
      })
    ;
  }
  
  Calculator.prototype.calculate = function(values) {
    // what do you need to do here?
    return values.reduce(function(res, value, i) {
      if(i === 2) {
        res = res.concat('.');
      }
      return res.concat(value);
    }, "");
  };
  
  Calculator.prototype.onChange = function(field, event) {
    
    this.values = Array.prototype.map.call(this.fields, function(f) {
      return Number(f.value) || 0;
    });
    
    this._state.result = this.calculate(this.values);
    
    this.render();
  }
  
  Calculator.prototype.render = function() {
    this.viewport.value = Number(this._state.result).toFixed(this.decimals);
  }
  return Calculator;
})();

document.addEventListener('DOMContentLoaded', function() {
  var fields = document.querySelectorAll('.calc-field');
  var viewport = document.querySelector('#viewport');
  
  var calculator = new Calculator(fields, viewport);
  calculator.render();
});

1 个答案:

答案 0 :(得分:0)

你很少有“错别字”

  • root

  • 必须roo而不是roo.config()
  • )必须位于

    的行尾
    subMenu.add_command(label="New Project..."), comand=doNothing #
    subMenu.add_command(label="New"), comand=doNothing
    
  • 必须是command=而不是comand=(请参阅:mm

from tkinter import * 

def doNothing():
    print ("ok ok i won't...")

root = Tk()

menu = Menu(root)
root.config(menu=menu)

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)

subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)

root.mainloop()