2016-10-09 2 views
0

"="에 입력 키를 키 바인딩하려고합니다. 내가 입력 한 두 숫자 키를 누릅니다에 넣어 때 내가 지금 가지고있는 코드와"="코드로 돌아 가기 키 바인딩하는 방법 : Python

, 나는 오류가 발생하는 오류는 다음과 같습니다

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' 

임의의 윈도우 시작시 팝업 사실 그냥 증거입니다 나는 그것이 작동하지 않는다고 생각한다. 루트 창 문제가 해결되면 키 바인딩 문제를 해결할 수 있다고 생각합니다.

입력을 누르면 오류가 발생합니다. 문제를 해결하는 데 도움이되는 사람이라면 코드를 실행하면서 열리는 임의의 창이 있습니다. 정말 붙어있어주세요.

전반적인 문제는 : 나는이 "="엔트리가 그렇게가 입력받을 수 있도록 내 키보드의 키를 입력 결박 키 입력하는 방법을 모르겠어요.

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() 

KEYBIND이 작동한다 WAY

+1

오류를 직접 텍스트로 게시하십시오. 텍스트 사진을 게시하지 마십시오. – Carcigenicate

+0

그러면 작동하지 않는 것을 설명해야합니다. 나는 너의 사진을 보았는데, 거의 도움이되지 않았다. ** 정확하게 ** 어떤 일이 일어나길 기대하고 ** 정확히 ** 무엇이 잘못되었는지. 사진이 실제로 문제의 가장 좋은 묘사라고 생각하면 직접 여기에 게시해야합니다. 이미지가 다른 사이트에서 제거되면 질문의 의미가 없어집니다. 그럼에도 불구하고 그림의 동작이 무엇인지 잘못 설명해야합니다. – Carcigenicate

+0

좋아요. 이번에는 정말 분명했습니다. <3 – CodingSquirrel

답변

0

BY MY CODE의 아래쪽에있다. 나도 필요없는 두 번째 창을 없앴습니다.

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() 
관련 문제