2016-10-09 4 views
0

Enterキーを "="にキーバインドしようとしています。私が今持っているコードで"="コードへの戻り値をキーバインドする方法:Python

、私は私は2つの数字を入れて、Enterキーを押しますエラーを取得し、エラーは次のとおりです。

Exception in Tkinter callback 
Traceback (most recent call last): 
line 1550, in __call__ 
return self.func(*args) 

line 68, in <lambda> 
root.bind('<Return>', lambda x: Calculator.calcu('=')) 
TypeError: calcu() missing 1 required positional argument: 'entry_num' 

ランダムウィンドウが起動時にポップアップするということは、単に証拠であります私はそれが動作していないと思う。私は、ルートウィンドウの問題が修正された場合、キーバインドの問題を修正できると考えています。

私がenterを押すとエラーが表示されます。問題を解決しようとしている人に役立つ場合は、コードを実行すると開くウィンドウもあります。

問題全体は、次のとおりです。 私はインクルードは「=」に私のキーボード上のキーを入力してバインド鍵とエントリがそうすることによって入力され得るようにする方法を知りません。

This is what I get when i run my code(picture)

from __future__ import division 
from functools import partial 
from math import * 
import tkinter as tk 
from tkinter import * 
root=Tk() 
class Calculator(tk.Tk): 
def __init__(self):  
    tk.Tk.__init__(self) 
    self.buttons_layout()  
    self.title("Thomas's calc") 
    textb = Entry(root) 

def calcu(self,entry_num): 

    if entry_num == "=": 
     try: 
      total=eval(self.textb.get()) 
      self.textb.insert(tk.END," = "+str(total)) 
     except: 
      self.textb.insert(tk.END, " - Invalid Calculation - ") 


    elif entry_num == "del": 
     self.txt=self.textb.get()[:-1] 
     self.textb.delete(0,tk.END) 
     self.textb.insert(0,self.txt) 

    elif entry_num == "C": 
     self.textb.delete(0,END) 

    elif entry_num == "i":  
     infob= Toplevel(self.textb.insert(tk.END, "")) 
     infob = Label(infob, text="Thomas, Calculator").pack() 


    else: 
     if "=" in self.textb.get(): 
      self.textb.delete(tk.END)   
     self.textb.insert(tk.END, entry_num) 

def buttons_layout(self): 

    self.textb = tk.Entry(root, width=66, fg="white", bg="black", state="normal") #text color = fg // background colour bg // sets the entry box specs 
    self.textb.grid(row=0, column=0, columnspan=5) 

    buttona="groove"      

    ycol = 0       

    xrow = 1          

    button_list = ["1","2","3","+","C", 
        "4","5","6","-","del",  
        "7","8","9","/","", 
        "0",".","i","*","="] 

    for button in button_list:    
     calc1=partial(self.calcu, button) 
     tk.Button(root,text=button,height=4,command=calc1,bg="aquamarine", fg="red",width=10,state="normal",relief=buttona).grid(
      row=xrow, column=ycol) 
     ycol= ycol + 1 
     if ycol > 4: 
      ycol=0 
      xrow= xrow + 3 

class key_binding(): 
    root.bind('<Return>', lambda x: Calculator.calcu('=')) 

end=Calculator() 
end.mainloop() 
root.mainloop() 

キーバインドは、これは動作するはずWAY

+1

ここにエラーを直接テキストとして投稿してください。テキストの写真を投稿しないでください。 – Carcigenicate

+0

あなたは何がうまくいかないか説明する必要があります。私はあなたの写真を見て、それはほとんど役に立たない。状態**正確に**あなたが何が起こることを期待し、そして**正確に**何が間違っていたか。その写真が本当にあなたの問題の最良の記述だと思うなら、ここに直接投稿する必要があります。画像が他のサイトから削除された場合、あなたの質問は意味を失います。それにかかわらず、あなたは絵の中の行動がどういうものなのかを説明する必要があります。 – Carcigenicate

+0

さて、今回は本当にきれいにしました。<3 – CodingSquirrel

答えて

0

BY MY CODE OF下部にあります。私も必要でなかった第2のウィンドウを取り除いた

from __future__ import division 
from functools import partial 
from math import * 
import tkinter as tk 
from tkinter import * 
#root=Tk() 
class Calculator(tk.Tk): 
    def __init__(self):  
     tk.Tk.__init__(self) 
     self.buttons_layout()  
     self.title("Thomas's calc") 
     textb = Entry(self) 
     self.bind('<Return>', lambda x: self.calcu('=')) 

    def calcu(self,entry_num): 

     if entry_num == "=": 
      try: 
       total=eval(self.textb.get()) 
       self.textb.insert(tk.END," = "+str(total)) 
      except: 
       self.textb.insert(tk.END, " - Invalid Calculation - ") 


     elif entry_num == "del": 
      self.txt=self.textb.get()[:-1] 
      self.textb.delete(0,tk.END) 
      self.textb.insert(0,self.txt) 

     elif entry_num == "C": 
      self.textb.delete(0,END) 

     elif entry_num == "i":  
      infob= Toplevel(self.textb.insert(tk.END, "")) 
      infob = Label(infob, text="Thomas, Calculator").pack() 


     else: 
      if "=" in self.textb.get(): 
       self.textb.delete(tk.END)   
      self.textb.insert(tk.END, entry_num) 

    def buttons_layout(self): 

     self.textb = tk.Entry(self, width=66, fg="white", bg="black", state="normal") #text color = fg // background colour bg // sets the entry box specs 
     self.textb.grid(row=0, column=0, columnspan=5) 

     buttona="groove"      

     ycol = 0       

     xrow = 1          

     button_list = ["1","2","3","+","C", 
         "4","5","6","-","del",  
         "7","8","9","/","", 
         "0",".","i","*","="] 

     for button in button_list:    
      calc1=partial(self.calcu, button) 
      tk.Button(self,text=button,height=4,command=calc1,bg="aquamarine", fg="red",width=10,state="normal",relief=buttona).grid(
       row=xrow, column=ycol) 
      ycol= ycol + 1 
      if ycol > 4: 
       ycol=0 
       xrow= xrow + 3 

#class key_binding(): 
# root.bind('<Return>', lambda x: Calculator.calcu('=')) 

end=Calculator() 
end.mainloop() 
#root.mainloop() 
関連する問題