Dec-04-2019, 07:40 PM
Hi,
Im new with coding and python and try to rebuild a scientific calculator.
I have the buttons and screen etc, but now i want to add commands to the buttons.
When i run the code and press one of the number buttons it wont show up in the window.
Every hint is welcome!
Im new with coding and python and try to rebuild a scientific calculator.
I have the buttons and screen etc, but now i want to add commands to the buttons.
When i run the code and press one of the number buttons it wont show up in the window.
Every hint is welcome!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
from tkinter import * import math import parser import tkinter.messagebox mainFr = Tk() mainFr.title( "CalcPy v1.1" ) operator = "" mainFr.configure(background = "gray8" ) mainFr.resizable(width = FALSE, height = FALSE) mainFr.geometry( "440x500+0+0" ) calc = Frame(mainFr) calc.grid() class Rekenmachine(): def __init__( self ): self .total = 0 self .current = "" self .input_value = True self .check_sum = False self .op = "" self .results = False def numberEnter( self , num): self .reuslt = False firstnum = txtDisplay.get() secondnum = str (num) if self .input_value: self .current = secondnum self .input_value = False else : if secondnum = = '.' : if secondnum in firstnum: return self .current = firstnum + secondnum self .display( self .current) def display( self , value): txtDisplay.delete( 0 , END) txtDisplay.insert( 0 , value) added_value = calc #===============DISPLAY================================== txtDisplay = Entry(calc, font = ( "arial" , 20 , "bold" ), bd = 10 , width = 28 , justify = RIGHT) txtDisplay.grid(row = 0 , column = 0 , columnspan = 4 , pady = 1 ) txtDisplay.insert( 0 , "0" ) #===============BUTTON BASIC=================================== numberpad = "789456123" i = 0 btn = [] for j in range ( 2 , 5 ): for k in range ( 3 ): btn.append(Button(calc, width = 6 , height = 2 , font = ( "arial" , 20 , "bold" ), bg = "gray75" , text = numberpad[i])) btn[i].grid(row = j, column = k, pady = 1 ) #================LAST ADDED CODE ====================================================== btn[i] [ "command" ] = lambda x = numberpad[i]: added_value.numberEnter(x) #======================================================================================= i + = 1 btnClear = Button(calc, text = chr ( 67 ), width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "OrangeRed2" ).grid(row = 1 , column = 0 , pady = 1 ) btnClall = Button(calc, text = chr ( 67 ) + chr ( 69 ), width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "OrangeRed3" ).grid(row = 1 , column = 1 , pady = 1 ) btnRoot = Button(calc, text = "√" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 1 , column = 2 , pady = 1 ) btnAdd = Button(calc, text = "+" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 1 , column = 3 , pady = 1 ) btnMin = Button(calc, text = "-" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 2 , column = 3 , pady = 1 ) btnDef = Button(calc, text = "/" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 3 , column = 3 , pady = 1 ) btnX = Button(calc, text = "x" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 4 , column = 3 , pady = 1 ) btnZero = Button(calc, text = "," , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 5 , column = 0 , pady = 1 ) btnDot = Button(calc, text = "0" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "gray75" ).grid(row = 5 , column = 1 , pady = 1 ) btnSpec = Button(calc, text = chr ( 177 ), width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 5 , column = 2 , pady = 1 ) btnEq = Button(calc, text = "=" , width = 6 , height = 2 ,font = ( "arial" , 20 , "bold" ), bg = "orange" ).grid(row = 5 , column = 3 , pady = 1 ) |