tkinter计算器等的退格键

时间:2017-06-02 03:17:31

标签: python python-2.7 tkinter

我对Python比较陌生,我目前正在使用GUI Tkinter计算器。我的问题是: -

1)我想添加一个退格按钮,删除数字的最后一位数字。例如543变为54.我添加了按钮,但我不知道如何为它定义一个功能。所以如果有人能告诉我在哪里写这个函数以及写些什么。

2)当我点击GUI按钮时,我的计算器工作正常但是当我手动输入整数输入时它不起作用。所以,如果有人能帮助我。

如果有人能帮我解决这两个问题,我将不胜感激。 这是代码:( P.S由于我复制粘贴而导致混淆不清)

    from Tkinter import *
    from ttk import *
    from ttk import Entry
    def keyPress(event):
          if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
           return True
elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
    return 'break'



class Example(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)   
    self.number1 = []
    self.operand_number = ''
    self.parent = parent
    self.operand1 = ''
    self.result = ''
    self.initUI()

def calculation(self, number):
    self.number1.append(str(number))
    self.operand1 = ''.join(self.number1)
    self.entry.delete(0,35)
    self.entry.insert(0, self.operand1)
    print 'pressed : ' + self.operand1 





def initUI(self):

    self.parent.title("Calculator")

    Style().configure("TButton", padding=(0, 5, 0, 5), 
        font='serif 10')


    self.columnconfigure(0, pad=3)
    self.columnconfigure(1, pad=3)
    self.columnconfigure(2, pad=3)
    self.columnconfigure(3, pad=3)

    self.rowconfigure(0, pad=3)
    self.rowconfigure(1, pad=3)
    self.rowconfigure(2, pad=3)
    self.rowconfigure(3, pad=3)
    self.rowconfigure(4, pad=3)

    self.entry = Entry(self)
    self.entry.grid(row=0, columnspan=4, sticky=W+E)
    self.entry.bind('<KeyPress>', keyPress)
    self.entry.focus()

    clear = Button(self, text="Clear", command=self.clear)
    clear.grid(row=1, column=0)
    bck = Button(self, text="Back")
    bck.grid(row=1, column=1, columnspan=2, sticky=N+W+S+E)
    clo = Button(self, text="Close", command=self.close_window)
    clo.grid(row=1,column=3,)        
    sev = Button(self, text="7", command =lambda: self.calculation(7))
    sev.grid(row=2, column=0)        
    eig = Button(self, text="8", command =lambda: self.calculation(8))
    eig.grid(row=2, column=1)         
    nin = Button(self, text="9",  command =lambda: self.calculation(9))
    nin.grid(row=2, column=2) 
    div = Button(self, text="/", command=lambda: self.operation("/"))
    div.grid(row=2, column=3) 

    fou = Button(self, text="4",command =lambda:  self.calculation(4))
    fou.grid(row=3, column=0)        
    fiv = Button(self, text="5",command =lambda:  self.calculation(5))
    fiv.grid(row=3, column=1)         
    six = Button(self, text="6", command =lambda: self.calculation(6))
    six.grid(row=3, column=2) 
    mul = Button(self, text="*", command=lambda: self.operation("*"))
    mul.grid(row=3, column=3)    

    one = Button(self, text="1", command =lambda: self.calculation(1))
    one.grid(row=4, column=0)        
    two = Button(self, text="2", command =lambda: self.calculation(2))
    two.grid(row=4, column=1)         
    thr = Button(self, text="3", command =lambda: self.calculation(3))
    thr.grid(row=4, column=2) 
    mns = Button(self, text="-", command=lambda: self.operation("-"))
    mns.grid(row=4, column=3)         

    zer = Button(self, text="0", command =lambda: self.calculation(0))
    zer.grid(row=5, column=0)        
    dot = Button(self, text=".", command =lambda: self.calculation("."))
    dot.grid(row=5, column=1)         
    equ = Button(self, text="=" , command =lambda: self.solution())
    equ.grid(row=5, column=2) 
    pls = Button(self, text="+", command=lambda: self.operation("+"))
    pls.grid(row=5, column=3)


    self.pack()

def close_window(self):
    self.parent.destroy()



def operation(self,symbol):
    self.symbol = symbol
    self.entry.delete(0,35)
    self.operand_number = ''.join(self.number1)
    del self.number1[:]


def solution(self):
    if(self.symbol == '+'):
        self.result = self.addition(float(self.operand1), float(self.operand_number))
    if(self.symbol == '-'):
         self.result = self.subtraction(float(self.operand1), float(self.operand_number))

    if(self.symbol == '*'):
         self.result = self.multiplication(float(self.operand1), float(self.operand_number))

    if(self.symbol == '/'):
         self.result = self.division(float(self.operand1), float(self.operand_number))

    self.entry.delete(0,35)
    self.entry.insert(0, self.result)
    self.operand1 = ''
    self.operand_number = ''
    print self.result






def addition(self,num1,num2):
    return num1 + num2

def subtraction(self,num1,num2):
    return num2 - num1

def multiplication(self,num1,num2):
    return num1 * num2

def division(self,num1,num2):
    return num2 / num1     

def clear(self):
    self.entry.delete(0,35)
    del self.number1[:]


 def main():

root = Tk()
app = Example(root)
root.mainloop()  


  if __name__ == '__main__':
    main()

提前致谢

1 个答案:

答案 0 :(得分:1)

1.问题1,请检查后退功能:

 def back(self):
    strStr = self.entry.get()
    self.entry.delete(0, 35)
    self.entry.insert(0, strStr[0: len(strStr) - 1])
    print(self.entry.get())

2.问题2,您需要在return函数更改keyPress

def keyPress(event):
    if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
           print(event.char)
    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        return 'break'

3.整个代码:

from Tkinter import *
from ttk import *
from ttk import Entry


def keyPress(event):
    if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
           print(event.char)
    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        return 'break'


class Example(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.number1 = []
        self.operand_number = ''
        self.parent = parent
        self.operand1 = ''
        self.result = ''
        self.initUI()

    def calculation(self, number):
        self.number1.append(str(number))
        self.operand1 = ''.join(self.number1)
        self.entry.delete(0,35)
        self.entry.insert(0, self.operand1)
        print 'pressed : ' + self.operand1


    def initUI(self):

        self.parent.title("Calculator")

        Style().configure("TButton", padding=(0, 5, 0, 5),
            font='serif 10')


        self.columnconfigure(0, pad=3)
        self.columnconfigure(1, pad=3)
        self.columnconfigure(2, pad=3)
        self.columnconfigure(3, pad=3)

        self.rowconfigure(0, pad=3)
        self.rowconfigure(1, pad=3)
        self.rowconfigure(2, pad=3)
        self.rowconfigure(3, pad=3)
        self.rowconfigure(4, pad=3)

        self.entry = Entry(self)
        self.entry.grid(row=0, columnspan=4, sticky=W+E)
        self.entry.bind('<KeyPress>', keyPress)
        self.entry.focus()

        clear = Button(self, text="Clear", command=self.clear)
        clear.grid(row=1, column=0)

        bck = Button(self, text="Back", command=self.back)
        bck.grid(row=1, column=1, columnspan=2, sticky=N+W+S+E)

        clo = Button(self, text="Close", command=self.close_window)
        clo.grid(row=1,column=3,)

        sev = Button(self, text="7", command =lambda: self.calculation(7))
        sev.grid(row=2, column=0)

        eig = Button(self, text="8", command =lambda: self.calculation(8))
        eig.grid(row=2, column=1)
        nin = Button(self, text="9",  command =lambda: self.calculation(9))
        nin.grid(row=2, column=2)

        div = Button(self, text="/", command=lambda: self.operation("/"))
        div.grid(row=2, column=3)

        fou = Button(self, text="4",command =lambda:  self.calculation(4))
        fou.grid(row=3, column=0)
        fiv = Button(self, text="5",command =lambda:  self.calculation(5))
        fiv.grid(row=3, column=1)
        six = Button(self, text="6", command =lambda: self.calculation(6))
        six.grid(row=3, column=2)
        mul = Button(self, text="*", command=lambda: self.operation("*"))
        mul.grid(row=3, column=3)

        one = Button(self, text="1", command =lambda: self.calculation(1))
        one.grid(row=4, column=0)
        two = Button(self, text="2", command =lambda: self.calculation(2))
        two.grid(row=4, column=1)
        thr = Button(self, text="3", command =lambda: self.calculation(3))
        thr.grid(row=4, column=2)
        mns = Button(self, text="-", command=lambda: self.operation("-"))
        mns.grid(row=4, column=3)

        zer = Button(self, text="0", command =lambda: self.calculation(0))
        zer.grid(row=5, column=0)
        dot = Button(self, text=".", command =lambda: self.calculation("."))
        dot.grid(row=5, column=1)
        equ = Button(self, text="=" , command =lambda: self.solution())
        equ.grid(row=5, column=2)
        pls = Button(self, text="+", command=lambda: self.operation("+"))
        pls.grid(row=5, column=3)


        self.pack()

    def close_window(self):
        self.parent.destroy()



    def operation(self,symbol):
        self.symbol = symbol
        self.entry.delete(0,35)
        self.operand_number = ''.join(self.number1)
        del self.number1[:]


    def solution(self):
        if(self.symbol == '+'):
            self.result = self.addition(float(self.operand1), float(self.operand_number))
        if(self.symbol == '-'):
             self.result = self.subtraction(float(self.operand1), float(self.operand_number))

        if(self.symbol == '*'):
             self.result = self.multiplication(float(self.operand1), float(self.operand_number))

        if(self.symbol == '/'):
             self.result = self.division(float(self.operand1), float(self.operand_number))

        self.entry.delete(0,35)
        self.entry.insert(0, self.result)
        self.operand1 = ''
        self.operand_number = ''
        print self.result

    def addition(self,num1,num2):
        return num1 + num2

    def subtraction(self,num1,num2):
        return num2 - num1

    def multiplication(self,num1,num2):
        return num1 * num2

    def division(self,num1,num2):
        return num2 / num1

    def clear(self):
        self.entry.delete(0,35)
        del self.number1[:]

    def back(self):
        strStr = self.entry.get()
        self.entry.delete(0, 35)
        self.entry.insert(0, strStr[0: len(strStr) - 1])
        print(self.entry.get())

def main():
    root = Tk()
    app = Example(root)
    root.mainloop()


if __name__ == '__main__':
    main()